Range through content type from single template

Hi friends,

I’m working on a template where I have projects and lessons. A project can have multiple lessons and a one lesson could be used in two projects.

The problem I’m trying to solve is to display all lessons that belong to a project in a list. While this code works from index.html it doesn’t work from the project single.html. The scope I’m in is a projects data.

{{- range (where .Data.Pages "Section" "lesson") }}
{{ end }}

Code

While I don’t get an error, I also don’t see any content.

What I planned to do is to have a variable lessons = [“lesson1”, “lesson2”] etc.
I’m open to new approaches on how to solve this problem.

Cheers,
Onur

Changing Data to Site gives me the lessons. Now I need to add the checks and logic to add lessons.

Glad you figured this out. .Data is a for list pages (i.e., nodes) that represents the data (pages, or the list of pages) available to them. The one caveat is that the homepage can use .Data.Pages for all pages across the site. A single page doesn’t have .Data.Pages because a single page, by default, doesn’t have any pages underneath it.

Hopefully this explanation of Hugo lists helps make things a bit clearer for future reference:

https://hugodocs.info/templates/lists/

Also keep in mind that from within a section page, you can use the even short {{range .Pages}}

HTH…

1 Like

Thanks @rdwatters, definitely helps!

Also, when doing where ranges in the single template, I would look into using the partialCached func, for performance reasons.

1 Like

Ooo. Good point. For reference, @onur

https://hugodocs.info/functions/partialcached/

For reference sake @bep does “for performance reasons” relate to Hugo’s compile time?

I call it “build time” … or time to build a site, whatever. But yes, it is directly related to that.

Thank you. Always…

Or, more appropriately, #ThankHu.