.Page.Resources directory from a _index.md shortcode

Page resources are available for page bundles only, i.e. a directory with either a index.md , or _index.md file at its root. Resources are only attached to the lowest page they are bundled with, and simple which names does not contain index.md are not attached any resource.

the above documentation indicates that I can bundle a bunch of image files in a directory at the same level as an _index.md or index.md file and access it through a shortcode. It works with index.md files but I cannot access the images unless they are spilled into the same directory as the _index.md file.

It would be nice to avoid having an exception for ‘list’ pages. I also have a fetish for wrapping everything in directories for better management by absent minded maintainers.

Any pointers appreciated.

Sample shortcode:

{{ $_path := ( .Get "path" ) | default "gallery/pic*.jpg" }}

{{ warnf "Checking %q for pictures..." $.Page.File.Path }}

{{ with .Page.Resources.Match $_path }}
  {{ warnf "Located picture gallery at %q" $_path }}
  {{ partial "collections/gallery/c" . }}
{{ else }}
  {{ warnf "Failed to get picture gallery at %q" $_path }}
{{ end }}

{{ with .Inner }}
  {{ . | markdownify }}
{{ end }}

compilation output:

Start building sites … 
WARN 2021/01/22 15:50:25 Checking "about/lead/index.md" for pictures...
WARN 2021/01/22 15:50:25 Located picture gallery at "gallery/pic*.jpg"
WARN 2021/01/22 15:50:25 Checking "services/_index.md" for pictures...
WARN 2021/01/22 15:50:25 Located picture gallery at "pic*.jpg"
WARN 2021/01/22 15:50:28 Checking "about/_index.md" for pictures...
WARN 2021/01/22 15:50:28 Failed to get picture gallery at "gallery/pic*.jpg"

content tree:

$ tree
.
|-- _index.md
|-- about
|   |-- _assets
|   |   `-- pic.jpg
|   |-- _index.md
|   |-- affiliations
|   |   `-- index.md
|   |-- pic.jpg
|   |-- gallery
|   |   |-- pic1.jpg
|   |   |-- pic2.jpg
|   |   |-- pic3.jpg
|   |   |-- pic4.jpg
|   |   `-- pic5.jpg
|   |-- lead
|   |   |-- gallery
|   |   |   |-- pic1.jpg
|   |   |   |-- pic2.jpg
|   |   |   |-- pic3.jpg
|   |   |   |-- pic4.jpg
|   |   |   `-- pic5.jpg
|   |   `-- index.md
|   `-- follow
|       `-- index.md
|-- contact-us
|   `-- index.md
|-- impressum
|   |-- _index.md
|   |-- copyright
|   |   `-- index.md
|   |-- legal
|   |   `-- index.md
|   |-- privacy
|   |   `-- index.md
|   `-- sitemap
|       `-- index.md
`-- services
    |-- _index.md
    |-- commercial
    |   |-- chaplin.jpg
    |   `-- index.md
    |-- corporate
    |   |-- chaplin.jpg
    |   `-- index.md
    |-- gallery
    |-- moarr
    |   |-- pic.jpg
    |   `-- index.md
    |-- pic1.jpg
    |-- pic2.jpg
    |-- pic3.jpg
    |-- pic4.jpg
    |-- pic5.jpg
    `-- lesss
        |-- pic.jpg
        `-- index.md

In short: No. you can’t access page bundle ressources from outside the page bundle. Depending on what you really are trying to achieve (I am not sure I get it fully) you have three options:

  1. rethink your content structure. I could think of a index.md (no _index.md) in a folder along other title1.md and so on files. Those files all could then access their “local” page bundle resources. I did not test this, but am pretty sure it works.

  2. You should be able to retrieve images from the assets folder via {{ $image := resources.Get "images/imagename.jpg" }} and then process them as you would in a page bundle. Put all galleries into the assets folder? Might be an option?

  3. There is always .GetPage. With this you could for instance put an index.md inside of the gallery page bundles and access them via `{{ $page := .GetPage “about/gallery” }}

the documentation states that _index.md and index.md directories are the same in this regard:

A Page Bundle can be one of:

Leaf Bundle (leaf means it has no children)
Branch Bundle (home page, section, taxonomy terms, taxonomy list)

Yet I cannot access a top level asset directory in the branch bundle but I can access an asset directory in a leaf bundle. The shortcode can read the picture gallery in about/lead/index.md but not in about/index.md, where it returns null:

|-- about
|   |-- _assets
|   |   `-- pic.jpg
|   |-- _index.md
|   |-- affiliations
|   |   `-- index.md
|   |-- pic.jpg
|   |-- gallery
|   |   |-- pic1.jpg
|   |   |-- pic2.jpg
|   |   |-- pic3.jpg
|   |   |-- pic4.jpg
|   |   `-- pic5.jpg
|   |-- lead
|   |   |-- gallery
|   |   |   |-- pic1.jpg
|   |   |   |-- pic2.jpg
|   |   |   |-- pic3.jpg
|   |   |   |-- pic4.jpg
|   |   |   `-- pic5.jpg
|   |   `-- index.md

further down in the documentation:

leaf bundle: Where can the Resources live? At any directory level within the  directory.
branch bundle: Only in the directory level **of** directory i.e. the directory containing the `_index.md` ([ref](https://discourse.gohugo.io/t/question-about-content-folder-structure/11822/4?u=kaushalmodi)).

seems implied that directories without an index.md are still considered a leaf bundle here. I thought bundles required the directory to contain an index.md or _index.md`. A confirmation would be nice.

That might be phrased wrong or unclear. A directory with _index.md is not a page bundle but a section or subsection. Look at the table here: Page Bundles | Hugo

(I am not sure, why I have the term “page bundle” in my head, but it clearly is named “leaf bundle” in the docs. If a directory has an index.md it has no subfolders with _index.md and everything inside this directory is available as “leaf bundle”.)

Did you try idea 3? That seems to me the closest to get the functionality you are seeking.

I’ll go for that workaround then. Thanks for your time.

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