Dynamically get a resource from menu entry

Why

{{ (resources.Get "icons/house.svg").Content | safeHTML }}

is ok, but

{{ $pre := .Pre }}
{{ $pre = print "icons/" $pre ".svg" }}
{{ $pre = (resources.Get $pre).Content | safeHTML }}

with Pre: "house" in a menu entry is not ok? I’ve got this error:

(...) error calling Get: CreateFile (...)\assets\icons\<svg class="bi bi-house" (...) <\svg>.svg: Incorrect filename or directory or volume synthax.

How can I dynamically get a resource from a menu entry?

Are you 100% sure you always have an SVG matching your search in your asset dir? Wrap your .Get with a with to make sure…

{{ $pre := .Pre }}
{{ $pre = print "icons/" $pre ".svg" }}
{{ with resources.Get $pre }}
  {{ .Content | safeHTML }}
{{ end }}

Gee, I never knew about the print function …

Well I don’t really understand why it works, because I only call for SVGs that exists in the assets/icons dir, but it works, thanks a lot @regis :slight_smile:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.