Is there any way to output the actual source code from an SVG asset in Craft?
Rather than linking to it, I would like to embed the code in the page.
I tried a twig include, but I cannot include files outside the templates folder.
Thanks!
What about using <use> (see css-tricks article) to reference the SVG from the template instead of embedding the actual code?
That's what I do for SVGs currently. Difference is that I don't use Craft assets for this, but have a grunt task that concatenates all SVGs from a folder into one single SVG file with multiple symbols. Then I use a macro that outputs the SVG tag. Just modified that code to make it work with Craft assets, all you need to do is to add an assets meta field to your asset with a handle symbol (or similar) to reference the right shape.
{% macro svg(svgAssetModel, cssClasses) %}
<svg class="{{ cssClasses }}" title="{{ svgAssetModel.title }}">
<use xlink:href="{{ svgAssetModel.url }}#{{ svgAssetModel.symbol }}"></use>
</svg>
{% endmacro %}
{{ macros.svg(entry.svg.first, 'big-blue-symbol') }}
There is now a function called svg in Craft itself: https://docs.craftcms.com/v3/dev/functions.html#svg
svgin Craft itself: https://docs.craftcms.com/v3/dev/functions.html#svg – medoingthings Jun 04 '20 at 08:21