We can already add classes to SVGs with this function, but it would be great if we could add any attribute.
It would make it a lot easier to follow accessible SVG patterns, especially with #3937 and if there were some way to reference the title and/or desc elements added that way.
Just added a new |attr filter for Craft 3.3, which will work on any HTML element, not just SVGs.
{{ svg('@webroot/icons/wave.svg')|attr({
class: 'icon',
role: 'img',
'aria-labelledby': 'title desc'
}) }}
It supports the same attribute definition as the attr() function added in 3.2 鈥撀爐hey both use Html::renderTagAttributes() behind the scenes.
Docs here: https://github.com/craftcms/cms/blob/3.3/docs/dev/filters.md#attr
Most helpful comment
Just added a new
|attrfilter for Craft 3.3, which will work on any HTML element, not just SVGs.It supports the same attribute definition as the attr() function added in 3.2 鈥撀爐hey both use Html::renderTagAttributes() behind the scenes.
Docs here: https://github.com/craftcms/cms/blob/3.3/docs/dev/filters.md#attr