Select the first image to process from Bundle Page


Based on the docs, I have the following code to process with the first image of a bundle page:

{{ range first 1 (.Resources.ByType "image") }}<img src="{{ .RelPermalink }}" alt="{{ .Title }}">
{{ end }}

It works fine but I am wondering whether it’s a more convenient way to achieve that, i.e. having a frontmatter parameter such as cover : image.jpg to select only the cover image.

You can do something ala:

{{ with .Params.cover }}
{{ with $.Resources.GetMatch . }}
// image
{{ end }}
{{ end }}

With the above you could use wildcards in your front matter (e.g. “featured*”)

Thanks, but it seems not working. I added cover: image.jpg in frontmatter
Just wondering why it needs to have both {{ with .Params.cover }} and {{ with $.Resources.GetMatch . }}

Please post your code.

The code is as follows:

{{ with .Params.cover }}
	{{ with $.Resources.GetMatch . }}
	<img src="{{ .RelPermalink }}" alt="{{ .Title }}">
	{{ end }}
{{ end }}

and in frontmatter:

title: "Les News"
description: "Quoi de Neuf partout dans le Monde"
weight: 10
cover: happy_announcement_ac67.svg

The code is fine. If you are still having problems please post a link to the repository for your project, or to a repository that demonstrates the problem.

The outer conditional (with .Params.Cover) verifies that the cover paramater was set in front matter.

The inner conditional ($.Resources.GetMatch .) verifies that the resource exists.

Thanks for the explanation.
The code actually works

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.