Thanks. Works great. Revised code:
{{ $s := slice "add 5 1" "sub 6 2" "mul 7 3" "div 8 4" }}
{{ range $s }}
{{ $f := printf "%s.txt" (. | md5) }}
{{ $r := printf "{{ %s }}" . | resources.FromString $f }}
{{ $r = $r | resources.ExecuteAsTemplate $f . }}
{{ printf "<pre>%s = %s</pre>" . $r.Content | safeHTML }}
{{ end }}
Note that resources.FromString
does not overwrite an existing file when ranging through the slice of commands or between rebuilds when running hugo server
. That’s why I initially used now.UnixNano
for the temporary file name.
When we use a hash value (md5
) for the temporary file name, the code above does not do what we think it does when our slice of commands contains duplicate items. For example:
{{ $s := slice "add 5 1" "add 5 1" }}
When processing the second item, resources.ExecuteAsTemplate
uses the resource created for the first item. But that’s OK because the expected result is the same.
Thank you very much for your help.