A page in .Site.Sections?

This works as expected

{{range .Site.Sections }}
  {{.Title}} {{.Kind}}</br>
{{end}}

Contact section
Experiences section
Hosts section
Media Folder section
Teams section
Values section

But

 {{range .Site.Sections }}
   {{.Title}}{{.Kind}}</br>
   {{range .Resources}}
     {{.Permalink}}
   {{end}}
{{end}}

leads to:

executing "main" at <.Resources>: can't evaluate field Resources in type *hugolib.Page

How did a Page make it into my sections?

Background: I am trying to make the resource bundle feature work for my usecase with netlifycms.

If anyone has encoutered this and can point me in the right direction that would be great!

The above does not seem right. What type of .Resources are you trying to range through? You need to use a method to return the given resources by type. See: Page resources | Hugo

Also everything in Hugo is a .Page including Section list pages.

If this does not clear things up, you need to show us more from your project like its content structure. So a link to your repository would help.

1 Like

.Resources is just a slice of resources. The (Get)Match methods are just to filter that slice. So if you donā€™t specify a resource filtering method, you get all the available resources.

1 Like

I see. But .Permalink is not available within the .Resources range. Correct?

What is your Hugo version?

Now, thereā€™s a qualified yes thereā€¦ itā€™s available only for non-page resources. I.e. you donā€™t have Permalink only for page resources in leaf bundles.

Each section is a Page too. Broadly speaking, Hugo has 2 kinds of pagesā€¦ pages that show the post content (like regular pages showing single blog posts), and list pages (like home page, section page, taxonomy pages, etc.). The list pages are typically used to link the regular/single pages. But with branch bundle _index.md files, you can also specify content and front-matter for such list pages.

I maintain a theme called hugo-bare-min-theme with ample debug info to help understand such Hugo internals. Hereā€™s the single.html template from there, that uses the .Resources (an example page using that layout).

1 Like

I think we need more examples with range and .Resources in the Docs. Like updating and moving the examples from the old (and currently outdated) Hugo 0.32 HOWTO

It would be better to have everything in one place (so that people like me donā€™t forget the basic stuff).

Iā€™m a bit busy with the GDPR stuff and Hugoā€™s internal shortcodes right now and I donā€™t have the time to contribute to the Docs currently, but if you donā€™t have the time for this Iā€™ll do it once I can.

Hmm, I just glanced through it and I didnā€™t find anything outdated in there.

It also has the {{ range . Resources }} example in there. But yes, Iā€™ll open an issue to move stuff from that HOWTO to proper docs.

.GetByPrefix is outdated.

1 Like

Cool! We are now tracking this issue for doc update here.

1 Like

To be specific (as this thread needs more chefs ā€¦): The **method is there ** on the Resource interface, but Permalink will return a blank string for the pages inside the bundles (that never gets written to disk and donā€™t have an URL).

My Hugo version is v0.40 darwin/amd64.

I am using hugo on a company project so I cannot point you to a repo.

I am trying to recreate this for my project:

The section I want to access is

content/
  media/
    _index.md
    img1.png
    img2.png

When I try to access the section with:

    {{ $page := . }}
    {{ with .Site.GetPage "section" "images" }}
      {{ with .Resources.GetByPrefix $page.Params.imagename }}
        {{ $image200x := (.Resize "200x") }}
        {{ $image400x := (.Resize "400x") }}
        <img src="{{ $image200x.RelPermalink }}">
        <img src="{{ $image400x.RelPermalink }}">
        <br />
      {{ end }}
    {{ end }}

I also get

executing "main" at <.Resources.GetByPref...>: can't evaluate field Resources in type *hugolib.Page

That is why I tried to understand my mistake by looping through all sections expecting them to contain at least an empty array of resources, but apartenly I understood something wrong there.

I havenā€™t tried to reproduce that errorā€¦ donā€™t know if that error is because that section page doesnā€™t have resources?

Try this:

{{ with .Resources }}
    {{ with .GetByPrefix $page.Params.imagename }}
    {{ end }}
{{ end }}

Correct. But folks can better help you if you create a dummy site recreating this issue, and then share that.

Itā€™s all guessing game till you do that.

But is a very good guess.

I have been a little confused by this myself.

can't evaluate field Resources in type *hugolib.Page

This error comes from the Golang template framework. *hugolib.Page has a Resources field, but since it is nil in this case, you get a slightly confusing error. I remember vaguely that methods behaves different from fields in this situation, so maybe we could make it into a method. It is perfectly fine to call methods on nil types.

1 Like

Thank you for being so responsive. Yes, I guess an empty array would be more expected in that case.

As for my specific problem, I did not share a ā€˜demoā€™ repository because the I cannot reproduce it outside of my project.

I think this means I have to investigate why the resources are empty on this specific section while there are image files co-located with the _index.md file as shown above. Just having image files in the same folder should be enough, right?

I think something ala cannot ... on nil *hugolib.Page would be better, because that is the source of the problem. An empty array/slice would indicate something different.

Not sure if this is relevant but Iā€™m getting the same error:
can't evaluate field Resources in type *hugolib.Page

I am trying to follow the instructions to use a headless bundle.

My bundle is:

/content/
    testimonial1.en.md
    testimonial2.en.md
    index.en.md

index.en.md contains only

headless: true

testimonial1.en.md contains

title: "..............SOME TEXT"
image: "/img/customers/soul-kitchen-logo.gif"

testimonial2.en.md contains

title: "..............SOME OTHER TEXT"
image: "/img/customers/another.gif"

and the layout file:

{{ $headless := .Site.GetPage "page" "testimonials" }}
{{ $reusablePages := $headless.Resources.Match "*" }}
{{ range $reusablePages }}
  <h3>{{ .Title }}</h3>
  <img src="{{ .Params.image }}" />
{{ end }}
1 Like