Page Resource access through variable

Hi there,

I am getting some troubles to access a page resource.
In my frontmatter, I defined a ressource called cover as follow

  name = "cover"
  src = "discover.jpg"
  title = "my cover title"

In my template, following code works perfectly :

{{ with .Resources.GetMatch "cover" }}
    {{ .RelPermalink }}
{{ end }}

However this code is not working and I do not understand why

{{ $image:= (.Resources.GetMatch "cover") }}
{{ $image.RelPermalink }}

Here is the error I get :
execute of template failed: template: blog/single.html:33:17: executing "main" at <$image.RelPermalink>: nil pointer evaluating resource.Resource.RelPermalink

I am using Hugo v0.74.3/extended.

Is there an explanation for this ?

because there is a page that doesnt have the “cover” resources.

the first method using with works, because with literally translate to IF THIS EXISTS THEN DO THIS


Thanks a lot pamubay.

I feel stupid that the solution was so easy… I had headaches checking variables context and so on :slight_smile:

1 Like

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