Skip to content

Instantly share code, notes, and snippets.

@piotrpog
Last active February 7, 2021 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piotrpog/b9218ab99455c275327a230b0999fb2e to your computer and use it in GitHub Desktop.
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
{%- 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