Last active
February 7, 2021 19:46
-
-
Save piotrpog/b9218ab99455c275327a230b0999fb2e to your computer and use it in GitHub Desktop.
Twig macro that loads and processes SVG image. More info: http://craftsnippets.com/articles/working-with-svg-images-in-craft-cms-templates
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
{%- macro svg(path, attributes = null, alt = null) -%} | |
{# v1 #} | |
{# settings #} | |
{% set directory = 'svg' %} | |
{# logic #} | |
{% if path is defined and path is not empty %} | |
{% set svg = svg('@webroot/'~directory~'/'~path~'.svg', namespace=true) %} | |
{% if alt %} | |
{% set svg = svg|prepend('<title>'~alt~'</title>') %} | |
{% endif %} | |
{% if attributes and attributes is not iterable %} | |
{% set svg = svg|attr({class: attributes}) %} | |
{% elseif attributes and attributes is iterable %} | |
{% set svg = svg|attr(attributes) %} | |
{% endif %} | |
{{svg|raw}} | |
{% endif %} | |
{%- endmacro -%} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment