$img stays pointing to nil. Getting .Params.image works as expected.
This pattern works for Pages and fails for ListPages and the HomePage.
After reading the docs, I would expect it to work for list pages, also: “This new model allows you to include list-specific front matter via .Params and also means that list templates have access to all page variables.”
thank you for your explaination. This clears up some things. Yet it makes me question many more (for my own answer, see after the EDIT tag):
Why is there any constraint on where my resources reside?
Is it any different when I explicitly list my resources (like in the example below)? Or do resources need to get listed in the front matter at all times (even if I do not have metadata for them; so there are no implicit resources)?
Conceptually I do not understand why this should not work:
I can’t find the relevant code. All I see is a generic globbing and loading of the specified files…
EDIT:
I think I just understood: When I say
Any page bundle (leaf or branch bundle) is only allowed to use its exclusive directories. It can not use resources located at a position that can be referenced by another page bundle. All page bundle resources are independet of their surrounding bundles and can be moved to arbitrary positions without breaking their resource references.
So a branch bundle can only use resources on its exact level in the directory hierachy. A leaf bundle can use resources on its own level or in any directory below.
The only exception from that rule are headless bundles, which can be the source of resources in other page bundles via a special mechanism to dereference them.
You don’t need to list your resources in your frontmatter. You do if you need to add metadata.
Essentially, yes.
Note that this example:
will not work because:
You cannot use templating logic directly in your content/markdown. You need to encapsulate it into a shortcode: Shortcodes | Hugo
Page .Resources have the following methods: (see .Resources.GetMatch) Page resources | Hugo
NOT to be confused with Hugo Pipes: (see resources.Get) Hugo Pipes | Hugo
The title is kind of misleading or I understood it differently.
I was looking to add feature or cover image on list page for each blog post. But the image is stored into page resources of each individual post. I didn’t want to add all images in static folder.
I found a hack solution that may be useful to some people.
Following code is inside range for pages slice.
This worked for me, thank you!
Is there a more elegant solution to this? The whole point of list pages is to showcase a summary from the main post and from what I see on the web a cover image is used pretty much all the time.
Another solution I found is to actually use this in a list/collection page (which doesn’t make much sense to have a workaround just to grab an image):
{{ range .Pages }}
<img src="{{ Rel.Permalink }}{{ .Params.image }}" alt=".Params.alt" >
{{ end }}
{{ .Rel.Permalink }} brings the url for your post, e.g. www.hugo.com/blog/my-post.
{{ .Params.image }} brings the url of the image inside the page bundle.
In frontmatter .image would be image = “/image/beautiful.jpg”.
Thank you so much for the hack it worked perfectly! Except I had to replace Rel.Permalink with .Permalink I have no idea why, but the following snippet worked for me:
{{ range .Pages }}
<img src="{{ .Permalink }}{{ .Params.image }}" alt=".Params.alt" >
{{ end }}