Use not literal string as parameter to .Page.Resources.GetMatch

Hi,

I’m having some issues with getting an image resource.

This works inside a shortcode and it does print the image link and both the images

{{ $imgPage := .Site.GetPage "/img" }}
{{ with (index .Page.Params (.Get "data")) }}
    {{ $background := $imgPage.Resources.GetMatch "image/background.jpg" }}
    <pre>{{ .background.RelPermalink }}</pre>
    <img src={{ .background.RelPermalink }} alt="background image">
    {{ $resized := $background.Resize "400x" }}
    <img src={{ .resized.RelPermalink }} alt="resized image">
{{ end }}

But when I replace the literal sting “image/background.jpg” with value from a front matter like this {{ $background := $imgPage.Resources.GetMatch .pathname_of_background }}
it gives me an error “at <$background.Resize>: nil pointer evaluating resource.Resource.Resize” pointing at {{ $resized := $background.Resize “400x” }}

{{ $imgPage := .Site.GetPage "/img" }}
{{ with (index .Page.Params (.Get "data")) }}
    {{ $background := $imgPage.Resources.GetMatch .pathname_of_background }}
    <pre>{{ .background.RelPermalink }}</pre>
    <img src={{ .background.RelPermalink }} alt="background image">
    {{ $resized := $background.Resize "400x" }}
    <img src={{ .resized.RelPermalink }} alt="resized image">
{{ end }}

doing

{{ $imgPage := .Site.GetPage "/img" }}
{{ with (index .Page.Params (.Get "data")) }}
    <pre>{{ .pathname_of_background }}</pre>
    {{ $background := $imgPage.Resources.GetMatch .pathname_of_background }}
    <pre>{{ $background | jsonify }}</pre>
{{ end }}

displays

image/background.jpg
{}

I tried using printf
{{ $background := $imgPage.Resources.GetMatch (printf “%s” .pathname_of_background) }}

but I still get the same error

So is there a way to get a resource by using data from front matter instead of using a string literal?
Since this code is going in a shortcode I need it to be flexible.

I’m pretty sure that your examples should just work, which mans that there is “something else going” on. Maybe some whitespace? What do you see if you do “

{{ printf “%q” .pathname_of_background }}

I get the value of .pathname_of_background but in quotes
“image/background.jpg”

I even tried
{{ $background := $imgPage.Resources.GetMatch (printf “%q” .pathname_of_background) }}
just in case but i get the same error.

just in case it might help. my hugo version is
Hugo Static Site Generator v0.56.1-0AD218AF linux/amd64 BuildDate: 2019-07-28T14:51:54Z

OK; my best guess then is that when you do

image/background.jpg
{}

It’s not actually nil.

Try to drop the jsonify.

when i do this

<div><pre>{{ .pathname_of_background }}</pre></div>
{{ $background := $imgPage.Resources.GetMatch (printf "%s" .pathname_of_background) }}
<div><pre>{{ $background }}</pre></div>

I get

image/background.jpg
Resource(image: image/background.jpg)

That’s good. but when i try to get the permalink (i need to put this in src of img tag) and immediately added this on the next line

   <div><pre>{{ $background.Permalink }}</pre></div>`

I again get this error

at <$background.Permalink>: nil pointer evaluating resource.Resource.Permalink

OK, now I think I understand.

I suspect the problem you have is that you have “many pages”, and some of them have params with a param that matches an image.

What you need to do is:

{{ $background := $imgPage.Resources.GetMatch (printf "%s" .pathname_of_background) }}
{{ with $background }}
<div><pre>{{ .RelPermalink }}</pre></div>
{{ end }}
1 Like

This works.

Thanks a lot. I’ve been working on this since yesterday trying to find a way to work around it.

1 Like

Also note that you can do something like this:

{{ $background := $imgPage.Resources.GetMatch "foo.jpg" | default $defaultBackground }}

To fall back to a default if not found.

1 Like

Thanks. that is worth considering. specially when I have to do some image image processing before I add it in html