.GetMatch troubles matching variable

I’m trying to use a front-matter param to define the path to an image that I want to resize. I can get it to work when I hard code in the file path, but when I try to define the file path with a variable, it gives me a nil pointer error when it tries to resize, so it seems like .GetMatch isn’t working like I expect.

This code works perfectly for me:

{{ $photo := (.Site.GetPage "/images").Resources.GetMatch "file/path.jpg" }}
{{ $resizedPhoto := ($photo.Resize "100x").Permalink }}
<img src="{{ $resizedPhoto }}"</img>

But this code doesn’t:

{{ $photoURL := "file/path.jpg" }}
{{ $photo := (.Site.GetPage "/images").Resources.GetMatch $photoURL }}
{{ $resizedPhoto := ($photo.Resize "100x").Permalink }}
<img src="{{ $resizedPhoto }}"</img>

nor does this:

{{ $photoURL := "file/path.jpg" }}
{{ $photo := (.Site.GetPage "/images").Resources.GetMatch (printf "%q" $photoURL }}
{{ $resizedPhoto := ($photo.Resize "100x").Permalink }}
<img src="{{ $resizedPhoto }}"</img>

Here’s the relevant error:

at <$photo.Resize>: nil pointer evaluating resource.Resource.Resize

In practice $photoURL would be defined by a param, but this illustrates my problem. All of the code examples are being run in the exact same spot, trying to eliminate context as the source of the promble. Any ideas?

The error is probably due to that parameter not being present in every file.

Check for the existence of $photoURL like so:

{{ $photoURL := < some-param > }}
{{ with $photoURL }} 
{{ end }}

If there are any context issues after wrapping your code in with then you can go higher up by using the dollar sign $ in front of a variable.

If the above does not solve your problem then you need to provide a repo with a simplified version of your project that reproduces the issue.