# How to build one collection for all nested sections?

**URL:** https://discourse.gohugo.io/t/how-to-build-one-collection-for-all-nested-sections/27950
**Category:** Uncategorized
**Created:** [September 1, 2020, 7:19am UTC](https://discourse.gohugo.io/t/how-to-build-one-collection-for-all-nested-sections/27950 "2020-09-01T07:19:44Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Troy](https://avatars.discourse-cdn.com/v4/letter/t/85f322/32.png) [@Troy](https://discourse.gohugo.io/u/Troy)
#### Post date: [September 1, 2020, 7:19am UTC](https://discourse.gohugo.io/t/how-to-build-one-collection-for-all-nested-sections/27950/1 "2020-09-01T07:19:44Z")

</div>

Based on [this answer](https://discourse.gohugo.io/t/pagination-for-nested-sections/27938/2) 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_

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

```

_children.html_

```auto
{{ $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](https://regisphilibert.com/blog/2017/04/hugo-scratch-explained-variable/) to get the work done.

---

<div class="post-metadata">

### Author: ![nfriedli](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/nfriedli/32/26027_2.png) [@nfriedli](https://discourse.gohugo.io/u/nfriedli)
#### Post date: [September 1, 2020, 7:57am UTC](https://discourse.gohugo.io/t/how-to-build-one-collection-for-all-nested-sections/27950/2 "2020-09-01T07:57:50Z")

</div>

Did you try something like:

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

```

The doc: [https://gohugo.io/templates/section-templates/#page-kinds](https://gohugo.io/templates/section-templates/#page-kinds)

---

<div class="post-metadata">

### Author: ![Troy](https://avatars.discourse-cdn.com/v4/letter/t/85f322/32.png) [@Troy](https://discourse.gohugo.io/u/Troy)
#### Post date: [September 1, 2020, 8:12am UTC](https://discourse.gohugo.io/t/how-to-build-one-collection-for-all-nested-sections/27950/3 "2020-09-01T08:12:46Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)
#### Post date: [September 1, 2020, 9:40am UTC](https://discourse.gohugo.io/t/how-to-build-one-collection-for-all-nested-sections/27950/4 "2020-09-01T09:40:47Z")

</div>

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

```auto
content
├── articles
│ ├── articles-1.md
│ └── articles-2.md
├── recipes
│ ├── recipes-1.md
│ └── recipes-2.md
└── _index.md

```

---

<div class="post-metadata">

### Author: ![Troy](https://avatars.discourse-cdn.com/v4/letter/t/85f322/32.png) [@Troy](https://discourse.gohugo.io/u/Troy)
#### Post date: [September 1, 2020, 9:51am UTC](https://discourse.gohugo.io/t/how-to-build-one-collection-for-all-nested-sections/27950/5 "2020-09-01T09:51:13Z")

</div>

```
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/](https://regisphilibert.com/blog/2017/04/hugo-scratch-explained-variable/)!

---

<div class="post-metadata">

### Author: ![nfriedli](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/nfriedli/32/26027_2.png) [@nfriedli](https://discourse.gohugo.io/u/nfriedli)
#### Post date: [September 1, 2020, 10:21am UTC](https://discourse.gohugo.io/t/how-to-build-one-collection-for-all-nested-sections/27950/6 "2020-09-01T10:21:43Z")

</div>

> [@Troy](#):
>
> In your solution I get all sections but only in alphabetical order.

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).

---

<div class="post-metadata">

### Author: ![Troy](https://avatars.discourse-cdn.com/v4/letter/t/85f322/32.png) [@Troy](https://discourse.gohugo.io/u/Troy)
#### Post date: [September 1, 2020, 10:25am UTC](https://discourse.gohugo.io/t/how-to-build-one-collection-for-all-nested-sections/27950/7 "2020-09-01T10:25:59Z")

</div>

Does your solution result in ONE collection?

I need this collection for pagination (as discussed [here](https://discourse.gohugo.io/t/pagination-for-nested-sections/27938/2)).

---

<div class="post-metadata">

### Author: ![nfriedli](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/nfriedli/32/26027_2.png) [@nfriedli](https://discourse.gohugo.io/u/nfriedli)
#### Post date: [September 1, 2020, 11:07am UTC](https://discourse.gohugo.io/t/how-to-build-one-collection-for-all-nested-sections/27950/8 "2020-09-01T11:07:13Z")

</div>

> [@Troy](#):
>
> Does your solution result in ONE collection?

No, sorry. I forgot the pagination you need ☹

---

<div class="post-metadata">

### Author: ![Troy](https://avatars.discourse-cdn.com/v4/letter/t/85f322/32.png) [@Troy](https://discourse.gohugo.io/u/Troy)
#### Post date: [September 1, 2020, 11:14am UTC](https://discourse.gohugo.io/t/how-to-build-one-collection-for-all-nested-sections/27950/9 "2020-09-01T11:14:47Z")

</div>

No problem. I always appreciate your help 🙂

---

<div class="post-metadata">

### Author: ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)
#### Post date: [September 3, 2020, 11:14am UTC](https://discourse.gohugo.io/t/how-to-build-one-collection-for-all-nested-sections/27950/10 "2020-09-03T11:14:56Z")

</div>

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