Access to Page Resources (simple)

Hi guys,

I’m a beginner with Hugo and I really like it. I’ve got a probably simple question, I looked for an answer way too long and I cannot find what I’m doing wrong.

I’ve got an image (boat.jpg) together with an index.md file in the same content directory. Inside the single.html template I want to access the image url in a single line. Like:

<img src="{{ (.Resources.GetMatch "boat.jpg").RelPermalink }}" />

But this gives the following error:

render of "page" failed: "C:\c\sites\bb\layouts\_default\single.html:24:39": execute of template failed: template: _default/single.html:24:39: executing "main" at <"boat.jpg">: nil pointer evaluating resource.Resource.RelPermalink

While writing it like this does work fine:
{{ with .Resources.GetMatch “boat.jpg” }}<img src="{{ .RelPermalink }}">{{ end }}

What is the difference, and can I make the first method working?

Thnx in advance

The above works because the code block is escaped if boat.jpg is not found.

You encounter the error:

render of "page" failed: "C:\c\sites\bb\layouts\_default\single.html:24:39": execute of template failed: template: _default/single.html:24:39: executing "main" at <"boat.jpg">: nil pointer evaluating resource.Resource.RelPermalink

Because not every Page rendered by the template _default/single.html has a boat.jpg as a resource.

You need to performs checks in a Hugo project’s templates, with is the function to use for such checks.

Wow, you’re fast!

When you explain it like that it makes sense :slight_smile:

I’ll use the second method and stop trying the first haha

Thanks for your answer!

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