Skip to content

Instantly share code, notes, and snippets.

@ivomynttinen
Last active February 7, 2017 06:51
Show Gist options
  • Save ivomynttinen/ff53b3b8f007efd0c4285942930075ef to your computer and use it in GitHub Desktop.
Save ivomynttinen/ff53b3b8f007efd0c4285942930075ef to your computer and use it in GitHub Desktop.
Kirby CMS Tag for srcset images. Supports the attributes "retina" (file name of the @2x resource), "alt" (alt text on img), "class" (additional class on the img element). Place under /site/tags/
<?php
kirbytext::$tags['srcset'] = array(
'attr' => array(
'retina',
'alt',
'class'
),
'html' => function($tag) {
$normal = $tag->attr('srcset');
$retina = $tag->attr('retina');
$alt = $tag->attr('alt');
$class = $tag->attr('class');
$html = '<figure><img src="';
$html .= $tag->page()->contentURL().'/'.$normal;
$html .= '"';
if($retina!=''){
$html .= ' srcset="';
$html .= $tag->page()->contentURL().'/'.$normal.' 1x, ';
$html .= $tag->page()->contentURL().'/'.$retina.' 2x"';
}
if ($alt!=''){ $html .= ' alt="'.$alt.'"'; };
if ($class!=''){ $html .= ' class="'.$class.'"'; };
$html .= '></figure>';
return $html;
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment