Images in my front matter are stored like this:
images:
- image: image_1.jpg
caption: "image 1 caption"
- image: image_2.jpg
caption: "image 2 caption"
I want to display just the first one, so I use the following:
{{ $image := .Resources.GetMatch (printf "**%s" (index .Params.images 0).image) }}
Oddly, this only works if I add the .image
part while my local hugo server is running. As soon as I stop and start it again, the site fails to rebuild with the following error:
... execute of template failed: template: partials/content/figure.html:7:70: executing "partials/content/figure.html" at <0>: can't evaluate field image in type string
I just want to be able to access images[0].image
. How can I make this work?
It turns out I had some other content types in which images
in front matter were still simple arrays, rather than arrays of objects. This was causing my build error. To solve the problem, I used the following, as was recommended here:
{{ $image := "" }}
{{ if reflect.IsMap (index .Params.images 0) }}
{{ $image = .Resources.GetMatch (printf "**%s" (index .Params.images 0).image) }}
{{ else }}
{{ $image = .Resources.GetMatch (printf "**%s" (index .Params.images 0)) }}
{{ end }}