How to build one collection for all nested sections?

Based on this answer I want to know how to build one collection for all nested section?

At the moment my setup looks like this, which prints a list of all (nested) sections:

list.html

  {{ range .Section }}
      {{ .Title }}
      {{ partial "children.html" . }}
  {{ end }}

children.html

{{ $child_pages := union .Sections .Pages }}
{{ range $child_pages }}
    {{ .Title }}
    {{ partial "children.html" . }}
{{ end }}

I want so save all nested sections in one (!) collection so that I can later range over it like

{{ range $allsections }}
    <a href="{{.Permalink}}">{{ .Title }}</a>
{{ end }}

After reading through the docs I think I might need Scratch to get the work done.

Did you try something like:

{{ range where .Site.Pages "Kind" "section"}}
    ...
{{ end }}

The doc: https://gohugo.io/templates/section-templates/#page-kinds

The order of the pages is important (I need it like in the tree…the same order you have in a prev/next navigation).

In your solution I get all sections but only in alphabetical order.

If you can’t share your repository, can you paste the ouput from tree content? Something like this?

content
β”œβ”€β”€ articles
β”‚   β”œβ”€β”€ articles-1.md
β”‚   └── articles-2.md
β”œβ”€β”€ recipes
β”‚   β”œβ”€β”€ recipes-1.md
β”‚   └── recipes-2.md
└── _index.md
content
- folder1/folder11/folder12
- folder2/folder21/folder22

…in every folder is an _index.md.

I think I have found the solution. Maybe there is an easier one?

allsections1.html

{{ $indexScratch := .Scratch }}
{{ range .Sections }}
  {{ $.Scratch.Add "sections" (slice . ) }}
  {{ partial "allsections2.html" (dict "indexScratch" $indexScratch "context" .) }}
{{ end }}

{{ range .Scratch.Get "sections" }}
<ol>
  <li>
    {{ .Title }}
  </li>
</ol>
{{ end }}

allsections2.html

{{ $indexScratch := .indexScratch }}
{{ $child_pages := union .context.Sections .context.Pages }}
{{ range $child_pages }}
  {{ $.indexScratch.Add "sections" (slice . ) }}
  {{ partial "allsections2.html" (dict "indexScratch" $indexScratch "context" .) }}
{{ end }}

Thanks @regis for the amazing tutorial on https://regisphilibert.com/blog/2017/04/hugo-scratch-explained-variable/!

The recursive β€œfunction”:

{{ define "subpages" }}
  {{ if .Pages }}
    {{ range .Pages}}
      {{ if eq .Kind "section" }}<a href="{{ .Permalink }}">{{ .Title }}</a><br>{{ end }}
      {{ template "subpages" . }}
    {{ end }}
  {{ end }}
{{ end }}

All pages from root sections:

{{ range .Site.Sections }}
  <a href="{{ .Permalink }}">{{ .Title }}</a><br>
  {{ template "subpages" . }}
{{ end }}

So you can choose the sort method you want (date, weight, etc.) in the range loops (for .Site.Sections and .Pages in subsections).

1 Like

Does your solution result in ONE collection?

I need this collection for pagination (as discussed here).

No, sorry. I forgot the pagination you need :frowning:

No problem. I always appreciate your help :slightly_smiling_face:

1 Like

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