Skip to content

Instantly share code, notes, and snippets.

@sakamies
Last active May 2, 2018 06:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sakamies/04dd80bb85b218b5f8e6 to your computer and use it in GitHub Desktop.
Save sakamies/04dd80bb85b218b5f8e6 to your computer and use it in GitHub Desktop.
Kirby icon tag with svg sprites
<?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