Nil pointer evaluating resource.Resource.Resize

Setup

My page bundle:

content
└── contact
    ├── index.md
    └── photo.jpg

index.md:

---
title: Contact
description: Where to find me
---
# Header

paragraph of text

![Photo](photo.jpg "Some nice photo")

I’m using a markdown render hook to process any inline images:

{{ $I := .Page.Resources.GetMatch .Destination }}

This works fine, and the image object can be queried for its properties.

Issue

If however, I invoke a method on the image:

{{ $I := .Page.Resources.GetMatch .Destination }}
{{ $S := $I.Resize "100x" }}

I get:

execute of template failed: template: _default/_markup/render-image.html:2:19: executing "_default/_markup/render-image.html" at <$I.Resize>: nil pointer evaluating resource.Resource.Resize

There are several topics with a similar error, but none resolved, at least not in a way that brings me forward.

Any help welcome, right now that’s a showstopper for me.


Update

This seems to be one of those sporadic failure cases that I tend to run into with Hugo. Sometimes it works, sometimes it doesn’t. If I empty the template, start the server, then reinstate the template, it works! But that’s misleading I think; Hugo cannot find the image from a cold start it seems.

If my way of fetching page resources is flaky, let me know!

Ack, I think I made a mistake: Another page had an inline image that I forgot about. That page didn’t have the matching page resource, so the image was not found.

I guess by updating the template with a running server, the other page did not get re-evaluated and so the error was masked from that point on.

1 Like

It’s always safer to assume the image might not exist. (Maybe on some pages it does not).

{{ with .Page.Resources.GetMatch .Destination }}
   {{ .Resize "100x" }}
{{ end }}
2 Likes

Oh cross messaging :wink:

Yes, same moment. But your point is correct. I had another page trigger the failure and didn’t realize. I added a with clause and now it works fine. Thanks!

1 Like