Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Twig macro that loads and processes SVG image. More info: http://craftsnippets.com/articles/working-with-svg-images-in-craft-cms-templates
{%- 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') %}
{% 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
You can’t perform that action at this time.