Last active
February 7, 2017 06:51
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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