I am attempting to implement an image processing shortcode that works for both leaf bundles and branch bundles. My img shortcode presently uses the .Page.Resources.GetMatch function to find a given resource and resize the image. This method works fine for leaf bundles, but fails to work for pages located in the root directory of a branch bundle.
content/page-bundle
- image.jpg
- index.md - shortcode works fine
content/branch-bundle
- image.jpg
- _index.md
- page.md - shortcode fails citing .Page.Resources is nil
From the documentation, it is not clear how I should go about accessing resources so that the shortcode functions in both situations. In particular, the Page Bundle documentation implies that I shouldn’t be able to use page resources, which I’ve interpreted to mean anything in a .Page.* context will not work from a branch bundle. Meanwhile, the Page Resources documentation implies that a call to .Resources.GetMatch should work for all pages; however, the .Resources variable is always undefined and will therefore fail.
Is there some general way to access resources that works for both leaf bundles and pages found within branch bundles?
The example is set up to build correctly by default, thereby demonstrating that the img shortcode, which uses .Page.Resources.GetMatch works correctly on the leaf page. However, if you set draft = false in the branch-bundle/branch-page.md the build will fail because the image is not registered as a resource despite being at the root of the branch bundle. Even if you change the shortcode to use .Resources.GetMatch the build still fails for the same reason.
Given the documentation cited above, I would think that image.png within the branch bundle should be a registered resources, yet it isn’t. Also, variants of .Parent.Resources and .Parent.Page.Resources fail for the same reason.
Am I just grossly misunderstanding how resources work in branch bundles?
branch-bundle/branch-page.mdcannot have page resources because it is not a page bundle. It cannot see branch-bundle/image.png because this image page resource belongs to branch-bundle/_index.md.
Ok. I understand better now. So a branch bundle is really meant to serve as a method to aggregate leaves or other branches, not a method to organize a bunch of related content that might share resources. So in my situation, I should convert branch-page.md and image.png into a leaf bundle instead. I’ll do that.
Thank you very much for the clarification. I’ll re-read the pages and consider sending in a PR to make the documentation more clear to the casual reader.
Indeed. Unfortunately I don’t think headless bundles quite matches my use case. I’m attempting to make a directory which contains all of my software. Each software product gets a page and typically has 1-2 images associated with it. I’m using the auto list feature of the branch bundle to neatly display them. If I have to make a few extra directories, it isn’t that big of a deal.