Skip to content

Instantly share code, notes, and snippets.

@girvan
Last active December 18, 2015 03:29
Show Gist options
  • Save girvan/5718987 to your computer and use it in GitHub Desktop.
Save girvan/5718987 to your computer and use it in GitHub Desktop.
viewport modifier
<?php
function smarty_modifier_viewport($content = null)
{
$html = '';
// no viewport
if(!empty($content))
$html = '<meta name="viewport" content="' . $content . '" />';
// TODO: don't forget caching issue
if(isset($_GET['fakepad']))
{
// no viewport
if(empty($content))
return '<meta name="viewport" content="width=980" />';
// width=device-width
if(stripos($content, 'device-width')!==false && stripos($content, 'initial-scale')===false)
return '<meta name="viewport" content="width=768" />';
// width=device-width, initial-scale=1.0
if(stripos($content, 'device-width')!==false && stripos($content, 'initial-scale')!==false)
{
$content = explode(',', $content);
$extra_content = array();
foreach($content as $attr)
if(!preg_match('/(device\-width|initial\-scale)/', $attr))
$extra_content[] = trim($attr);
$content = implode(',', $extra_content);
if(!empty($content))
$content .= ',';
$html = <<<HTML
<meta name="viewport" id="viewport" />
<script>
(function(s, o){
var pt = (o == 0),
w = pt ? 768 : 1024,
iscale = pt ? 320/w : 480/w;
document.getElementById('viewport').setAttribute('content', '{content}width=' + w + ',initial-scale=' + iscale);
})(screen, window.orientation);
</script>
HTML;
$html = str_replace('{content}', $content, $html);
return $html;
}
}
else
return $html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment