resources.Get throw error if argument is a shortcode parameter

Environment

hugo v0.87.0+extended linux/amd64 BuildDate=unknown

Issue

I’m trying to write a shortcode to insert images on pages so I can process them. I’m not using page bundles for content so I can’t use .Resources.GetMatch and resources.Get is just not working.

On the page…

{{< image src="/img/subdir/image.jpeg" alt="Description" width="640" height="400" >}}

Inside the shortcode…

…this work as expected:

{{ $src := "image.jpg" }} 
{{ $image := resources.Get $src }}
<img src="{{ $image.RelPermalink }}">

while this doesn’t:

{{ $src := .Get "src" }} 
{{ $image := resources.Get $src }}
<img src="{{ $image.RelPermalink }}">

throwing this error:

Error: Error building site: "/site/content/my-page.md:56:1": failed to render shortcode "image": failed to process shortcode: "/site/themes/my-theme/layouts/shortcodes/image.html:8:18": execute of template failed: template: shortcodes/image.html:8:18: executing "shortcodes/image.html" at <$image.RelPermalink>: nil pointer evaluating resource.Resource.RelPermalink

Try

 {{ $src := .Get "src" }} 
{{ $image := resources.Get $src }}
{{ with $image }}
<img src="{{ .RelPermalink }}">
{{ end }}
1 Like

It works when doing so. :smiley:

Async problem?

No, you pass some value(s) of src that does not resolve to a resource.

To elaborate on @bep’s answer: You probably call this short code from a lot of pages. At least on one of them, you pass in a string that does not point to an image (spelling error or something like that?). To help you with debugging, you might use this:

 {{ $src := .Get "src" }} 
{{ $image := resources.Get $src }}
{{ with $image }}
<img src="{{ .RelPermalink }}">
{{else}}
{{ errorf "Wrong image: %s" $src}}
{{ end }}

That will still throw an error, but at least you should see the offending string.

Oh I see. Thanks! And sorry for the trouble.

Thanks for the explanation. Liked the suggestion. I’m using something similar warnf to warn when referenced images are not found on the file system.