Readfile > Print inside range not executing correctly

Hi there,

I have the following code which checks to see if the image is an svg or other format and attempts to inline the SVG where possible.

{{ range .vendorLogos }}
      {{ $format := substr (index .) -1 3}}
      {{ if eq $format "svg"}}
      {{ $image := index . }}
      <img src="{{ $image }}" alt="{{ $image }}">
        {{ readFile (print "static/" $image) | safeHTML }}
      {{ else }}
        <img src="{{ index . }}" alt="{{ index . }}">
      {{ end }}
      {{ end }}

This line: {{ readFile (print "static/" $image) | safeHTML }} continually outputs the same logo but the line above it outputs correctly so I presume it’s an issue with the readFile function taking too long to execute or am I missing something? Is this a bug?

Do you have a repo to share? This is one of those questions where you’ll get better help if we can clone a repo and reproduce

1 Like

Unfortunately I can’t give you access to the repo. What else would be helpful?

Create a minimal reproducible example and share that

you try to encode the format from the file name. there are functions for it!

Use .File attribute of your page and read more about the file functions

{{ if eq .File.Ext "svg" }}

could do it!

{{ if eq .File.Ext "svg" }}

Isn’t this just for the parent document? Inside the {{range}} I get the following error:

<.File.Ext>: can't evaluate field File in type interface {}

Sorry, don’t know what your range is looping over … so your question ist to short :wink:
.File is a page attribute. If you range over files (used ReadDir ??), .Ext should do it.