Troy
1
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
Troy
3
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
Troy
5
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
Troy
7
Does your solution result in ONE collection?
I need this collection for pagination (as discussed here).
No, sorry. I forgot the pagination you need 
Troy
9
No problem. I always appreciate your help 
1 Like
system
Closed
10
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.