Can't get page resources through shortcode

I have a shortcode called debug.html in layouts/shortcodes/debug.html.

{{ with .Resources.ByType "image" }}
  {{ range . }}
    {{ .RelPermalink }}
  {{ end }}
{{ end }}

Note: I have also tried using .Page.Resources.

I have made a subfolder under /content/2020/test, containing an images/ folder and a index.md. In the images folder, I have a PNG file called basketball.png. Within index.md, I have called the shortcode:

{{> debug <}}

However, it returns an error saying:
execute of template failed: template: shortcodes/debug.html:1:18: executing "shortcodes/debug.html" at <.Resources.ByType>: can't evaluate field Resources in type *hugolib.ShortcodeWithPage

Do I have to define all page resources in the front matter of index.md before I can use them? Or what is the reason for this error? Thanks.

Could you try $.Page.Resources?

Ok never mind, I’m stupid. I forgot to name my file index.md and named it test.md instead. Maybe that’s why it couldn’t find the resources. .Page.Resources seems to work (without the ), although it works with the too. Strangely, the appears to be the global context according to the Hugo docs, so would putting the in the shortcode mess up the shortcode’s context (which should be the specific page context?)

Also, what’s the difference between .Resources and .Page.Resources in this context (leaf bundle)?

Nah, you’re right. I simply assumed the scope could be wrong. Sorry.

It could since you’re using .ByType (I’m using .GetMatch, a bit more specific).

Yep, I changed it to GetMatch after, the ByType was simply a debugging test because it didn’t work previously. Now if the image is not found, the entire Hugo build fails. Is it possible to wrap the entire shortcode with a fileExists while maintaining the original context?

Weird, shouldn’t with take care of that? But now I’m in the dark, I have less details than before.

Sorry about the confusion. Everything works correctly, it’s just that in case I type the “src” attribute wrongly in the shortcode (and the file cannot be found in resources), the entire build will fail. I’m trying to have it output something like “Image could not be found” in the shortcode template instead.

Yes, a with would indeed work, but I heard that with changes the context to within itself, meaning I will not be able to access other shortcodes variables passed in if I do something like:

{{ with (.Page.Resources.GetMatch (printf "**%s*" (.Get "src"))) }}

The context will then simply be the image itself, will it not? I would then not be able to do things like:

{{ with .Get "alt" }}
    alt='{{- . -}}'
{{ else }}
    alt=''
{{ end }}

Please correct me if I’m understanding the docs wrongly. Thank you!

Why don’t you define those outside the with…?

For reference: Define a Variable Independent of Context.

1 Like

Hmm, ok I’ll try that. Seems like an obvious idea, clearly I haven’t had enough sleep… thanks again for the patience!

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