Skip to content

Instantly share code, notes, and snippets.

@girvan girvan/gist:5718987
Last active Dec 18, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.