Last active
May 2, 2018 06:06
-
-
Save sakamies/04dd80bb85b218b5f8e6 to your computer and use it in GitHub Desktop.
Kirby icon tag with svg sprites
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['icon'] = array( | |
'attr' => array( | |
'rel', | |
'link', | |
'target', | |
'width', | |
'height', | |
'class', | |
'color', | |
'align' | |
), | |
'html' => function($tag) { | |
$icon = $tag->attr('icon'); | |
$width = $tag->attr('width'); | |
$height = $tag->attr('height'); | |
if ($width == '') { | |
$width = '36'; | |
} | |
if ($height == '') { | |
//icons are square by default | |
$height = $width; | |
} | |
$iconColor = ''; | |
if ($tag->attr('color')) { | |
$iconColor = '-'.$tag->attr('color'); | |
} | |
$iconUrl = url('assets/icons/'.$icon.$iconColor.'.svg'); | |
$iconAttrs = attr(array( | |
//'src' => $iconUrl, | |
'title' => $tag->attr('icon'), | |
'width' => $width, | |
'height' => $height, | |
'class' => 'icon ' . $tag->attr('class') .' '. $tag->attr('color').'-bg' | |
)); | |
// $iconElement = '<img '.$iconAttrs.' role="presentation">'; | |
$iconElement = '<svg '.$iconAttrs.' viewBox="0 0 '.$width.' '.$height.'">'; | |
$iconElement .= '<use xlink:href="'.url('assets/sprites/icons.svg#').$icon.'"></use>'; | |
$iconElement .= '</svg>'; | |
if ($tag->attr('link') != '') { | |
$linkAttrs = attr(array( | |
'rel' => $tag->attr('rel'), | |
'href' => $tag->attr('link'), | |
'target' => $tag->attr('target'), | |
'class' => 'icon-link' | |
)); | |
return '<a '.$linkAttrs.'>'.$iconElement.'</a>'; | |
} else { | |
return $iconElement; | |
} | |
} | |
); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment