Skip to content

Instantly share code, notes, and snippets.

@ivomynttinen

ivomynttinen/srcset.php

Last active Feb 7, 2017
Embed
What would you like to do?
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