Hello!
I’ve a frontmatter like this:
+++
name = "Robin"
photo = "robin.jpg"
+++
How can I load this resource in the template? I’ve tried many variations like:
{{ $image := .Resources.GetMatch (.Params.Photo) }}
I’ve also been searching for documentation which explains the syntax a bit. What I’ve found is https://gohugo.io/templates/introduction/ , but I think it doesn’t go far enough. Is there already someone working on that or should I just start sending some PRs?
Thank you in advance!
Robin
bep
April 13, 2018, 7:42pm
2
Not sure why the above does not work, but you can make it more flexible with printf
.
{{ $image := .Resources.GetMatch (printf "**%s" .Params.photo) }}
But.
I think it is better to either
Create a naming standard for yourself (“featured.jpg”)
Or combine that with the “resource metadata renaming” feature
@kaushalmodi should have some input on this.
1 Like
Did you miss this detailed example here:
Resize, crop, rotate, filter, and convert images.
Here’s another example from my Hugo Sandbox site… you can see that header_image.html
partial doing its magic to show that image on the Sandbox homepage.
1 Like
openscript:
photo = “robin.jpg”
Also, I encourage people to use the more “standard” images
list variable in the front-matter, as that’s what’s used in the inbuilt opengraph
and twitter_cards
templates.
So the equivalent would be:
images = ["robin.jpg"]
I use a slightly tweaked version of the twitter_cards.html
template , where you see the use of .Params.images
.
1 Like
Thank you for your quick answer.
Building sites … ERROR 2018/04/13 23:05:36 Failed to render "theme/section/about.html": reflect: Method on nil interface value
That’s the error message I get. Maybe I’m doing something else wrong? This is the whole template:
<section id='about'>
<div class='center-block'>{{ .Content }}</div>
{{ range .Pages }}
{{ .Params.Name }}
{{ .Params.Role }}
{{ $image := .Resources.GetMatch (.Params.Photo) }}
<img src="{{ $image.RelPermalink }}">
{{ end }}
</section>
What would you advice me? I get quite often stuck with such things. Is there an easy way to debug the source and learn from there?
Most likely, one of the pages doesn’t have that param set, and thus that nil related error.
I’d wrap the resource fetching in {{ with .Params.photo }}
to avoid this. You can refer to my example I linked earlier.
In my experience, using with
is always good.
Also, if the param is lower case photo
, don’t capitalize it in the .Params
call.
bep
April 13, 2018, 11:07pm
7
I suspect it is the image itself that is missing in some pages.
So this should be better:
{{ $image := .Resources.GetMatch (.Params.Photo) }}
{{ with $image }}
<img src="{{ .RelPermalink }}">
{{ end }}
2 Likes
Right… the error makes more sense considering that… it was actually trying to do “nil.RelPermalink”…
Thank you for your answers.
I just don’t get it to work.
On the screenshot the template for this section is visible.
How does the resource matching looking for the image?
I’ve tried the following arguments:
robin.jpg
about/robin.jpg
**/robin.jpg
**/robin*
…
I’ve checked https://gohugo.io/content-management/page-resources/ . What am I doing wrong?
Robin
First of all, it’s not clear where you have that template. It should be list.html or similar because your about content is a branch bundle (_index.md).
If you have done that, the resource fetching must then live outside that range
of pages.
Try putting the .Resource.GetMatch
before that range
line.
If it doesn’t yet work, please share your site source.
And… if you plan to have multiple people with their own photos in “about”, you need a structure like this:
content/
about/ (branch bundle)
_index.md
foo/ (leaf bundle)
index.md
person.jpg
bar/ (leaf bundle)
index.md
person.jpg
Then you can do the resource fetching for each leaf bundle inside that range
.
You need to understand the “context” you are in when inside that range
.
Awesome! Thank you very much for taking the time to give me this answer It works!