Leaf bundles not working in custom theme

Hi everyone,

I have a really weird issue with leaf bundles in my custom template. And before creating an issue I wanted to ask here if I am missing something. I uploaded my test project here: https://github.com/ser1us/hugo-test

Now, what I did is this:

  • started a new hugo site
  • added a new hugo theme
  • created a leaf bundle in the content folder called “uploads” with one jpg file

Then I added the following snippet into the single.html layout:

{{ $headless := .Site.GetPage "/uploads" }}
{{ with $headless }}
    {{ $headless }}
    {{ $headless.Resources }}
    {{ $imageResource := $headless.Resources.GetMatch "test.jpg" }}
    {{ with $imageResource }}
        {{ $resized := .Fill "100x100" }}
        <img src="{{ $resized.RelPermalink }}" />
    {{ end }}
{{ end }}

I wanted to have a look at the page and the resources inside. Now this is what happens:

Running hugo server, does not show me any page bundles. Now here comes the interesting part. I grab the uploads folder and move it out of /contents. I refresh the page, and nothing happens. When I move the folder back and refresh the page I can see one page bundle with no resources.

I did exactly the same thing with the ananke theme, and there it’s working immediately all the time. This looks to me I am missing something here?

Thanks a lot for your help!

Cheers,
Christoph

Hugo version:
Hugo Static Site Generator v0.46/extended darwin/amd64 BuildDate: 2018-08-01T09:09:50Z

A bundle is not a bundle unless it contains either a “index content file”, e.g “index.md”, “Index.html”, “_Index.md” etc.

What do you see when you change the index.md file in your repo from YAML to TOML and add the following debug statement, one above and one below, the {{ .Content }} statement in your single.html:

{{ printf "%#v" (.Site.GetPage "/uploads").Resources }}

You are right, the index.md file is there (with the headless: true setting), forgot to mention it.

Thanks for your reply.

I made the changes, switched to TOML:

+++
headless = true
+++

And updated my single.html to this

{{ printf "%#v" (.Site.GetPage "/uploads").Resources }}

{{- .Content -}}

{{ printf "%#v" (.Site.GetPage "/uploads").Resources }}

This results in this error message

Building sites … ERROR 2018/08/06 15:11:45 Error while rendering "page" in "": template: _default/single.html:1:31: executing "_default/single.html" at <"/uploads">: can't evaluate field Resources in type *hugolib.Page
Total in 23 ms
Error: Error building site: logged 1 error(s)

Cheers!

Thanks for the info. I saw the same issue earlier when I built your repo and tried to access the resources of your leaf bundle. Then I tried a jiggle the keys approach to debugging and renamed the index file from index.md to _index.md and back again. I also tried defining the main block in your single.html but learned it was unnecessary. After copying the single.html into index.html (which was empty when I tested) and doing the above I was able to get output from the printf.

I noticed the output from printf was different when placed before and after the {{ .Content }} statement, but in both cases .Resources output was nil, which to me seemed unexpected after reading up on headless page bundles. TBH I’m stumped on what could be causing the issue, which also happens in Hugo 0.44.

Hmmm, I also tried to copy the single.html into index.html but that didn’t change anything for me. Anyway, thanks a lot for the update and for taking the time to investigate!

The snippet works when moved to index.html and if you change content/index.md to content/_index.md

1 Like

Thank you very much! This works great now!

1 Like