[SOLVED] Inject an SVG file into my HTML

Just for the hell of it, @keauval, at the content level, you could do this as a shortcode:

<!--layouts/shortcodes/svg.html-->
{{$svg := .Get 0}}
{{ $svg  | readFile | safeHTML }}

Then within the body copy/content of *.md:

{{< svg "static/svgs/your-svg.svg" >}}

This assumes, obviously, that you’re keeping your svgs as static assets a la images.

As a matter of fact, you could do something similar if you were trying to create a sort of content “snippet” that you want to add in content (i.e., *.md) files using .GetPage. The following assumes you have a content/snippets folder.

Note: I’m doing this off the cuff and haven’t tested this like the above svg example:

<!--layouts/shortcodes/snippet.html-->
{{$snippet := .Get 0}}
{{ with .GetPage "page" "snippets" $snippet }}{{.Content}}{{end}} 

Then in your content file, you could call it like:

{{< snippet "my-snippet.md" >}}

You’d have to disable the snippets section, however, if you don’t want files in the snippets folder to render full-blown html files. (Again, not tested…but if anyone tries it, let me know.)

2 Likes