Nil pointer evaluating resource.Resource.Resize problem

Laura Kalbag’s image formatting looks so easy but I fail to see why it does not work for me even though I use page bundles. It fails at $src.Resize
If I remove the resize line it displays the image properly.

{{< img src=“alfred-with-award.jpg” alt=“Alfred Tuinman with the award”>}}

{{ $src := .Page.Resources.GetMatch (printf “%s” (.Get “src”)) }}
{{ $tinyw := default “500x” }}
{{ .Scratch.Set “tiny” ($src.Resize $tinyw) }}

Hugo v0.68.3
Ubuntu 20.04

I suspect that you have some pages that does not get a match.

Try

{{ $src := .Page.Resources.GetMatch (printf “%s” (.Get “src”)) }}
{{ if $src }}
{{ $tinyw := default “500x” }}
{{ .Scratch.Set “tiny” ($src.Resize $tinyw) }}
{{ end }}

src is just the file name but the system wants the .Permalink prefixed it seems so that the path is complete?

I gave you a piece of qualified advice, I suggest you try it before you reject it.

your piece of advise worked though I don’t understand why the {{ if $src }} makes it work as it is not as if $src changed. Anyway, I am delighted after wasting a few hours reading and trying. Pfff

The resized image is generated in resources as explained in the docs.

1 Like

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