Filter resources in subdirectories with Resources.Match

hugo

I’m trying to access the image “img-in-a.png” inside the subdirectory “a” (inside the content directory) with Resources.Match.

Using the homepage template (layouts/index.html) I expected this to work:

{{ $img := .Resources.Match "a/img-in-a.png" }}

But the result is empty.

{{ printf "%#v" $img }} prints resource.Resources(nil)

What am I doing wrong?

The project is available at: https://github.com/b12r/hugo-resources-match

PS: I’m using Resources.Match because I think it is the preferred method for a multilingual setup but I’m not there yet.

try {{ $img := .Resources.Match “**/img-in-a.png” }}

Thanks for your help. Unfortunately this doesn’t work either. Same result, i.e. empty result.

The project is now available at: https://github.com/b12r/hugo-resources-match

the home page is no page bundle - rename it to index.html

I’m confused. The documentation says the home page can be a branch bundle. According to the docs the name of the index file in branch bundles should be _index.md. You were talking about renaming the index file “_index.md” in content to “index.html”, am I right? Could you elaborate on your answer? Thanks again for your help.

i checked your repo, no idea - sorry

No sweat, thanks anyway.

As per the repo /a/index.md is a regular page. To fetch its resource img-in-a.png on the homepage first you need to call the regular pages page group with something like range .Site.RegularPages and then within this context you can fetch the image resource more-or-less as mentioned above.

To limit the scope of the page group I would suggest that you filter it by a parameter or anything else that suits your project’s needs.

Thanks for your detailed answer! I learned that Resources.Match doesn’t include other content directories which relate to pages.

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