Create a structured content listing (all pages recursively by section)

I have structure in hugo:

content
├── 1.md
├── a
│   ├── 2.md
│   └── 3.md
└── b
    └── 4.md

and I want menu list like:

  • 1
  • a
    • 2
    • 3
  • b
    • 4

I’m using now

<ul>
    {{ range .Site.RegularPages }}
        <li>
            <a href="{{ .Permalink }}">{{ .Name }}</a>
        </li>
    {{ end }}
</ul>

which gives me just:

  • 1
  • 2
  • 3
  • 4

I added

---
title: Aaaa
---

to _index.md in a section

structure

content
├── a
│   ├── 2.md
│   ├── 3.md
│   └── _index.md
├── b
│   ├── c
│   │   ├── 5.md
│   │   ├── 6.md
│   │   └── _index.md
│   ├── 4.md
│   └── _index.md
├── 1.md
└── _index.md

layouts/partials/walk.html

<ul>
  {{ range .Pages.ByTitle }}
    <li>
      <a href="{{ .RelPermalink }}">{{ .Title }}</a>
    </li>
    {{ if .Pages }}
      {{ partial "walk.html" . }}
    {{ end }}
  {{ end }}
</ul>

layouts/index.html (assumes you have a layouts/_default/baseof.html)

{{ define "main" }}
  <h1>{{ .Title }}</h1>
  {{ .Content }}
  {{ partial "walk.html" . }}
{{ end }}

Note that this excludes the home page.

Thank you! Awesome code. also I want to ask, is there way to list sections first and disable creation of list pages?

See:

I don’t understand your question. Please provide an example of what you wish to achieve.

Sorry for my english. (in menu) I want first render sections which has subpages and then standalone pages (sort in another way than .ByTitle). Also I want to prevent rendering list pages (_index.md) at all. Thanks for help again.

Sorry, but I still don’t understand what the final result should look like.

The code above produces this:

image

What do you want? Show me.

I haven’t tested this yet but the idea is step-by-step exactly like what you want:

<ul>
  {{ range .Pages.ByTitle }}
    {{ if .Pages }}
      <li>
        <a href="{{ .RelPermalink }}">{{ .Title }}</a>
      </li>
      {{ partial "walk.html" . }}
    {{ end }}
  {{ end }}
  {{ range .Pages.ByTitle }}
    {{ if not .Pages }}
      <li>
        <a href="{{ .RelPermalink }}">{{ .Title }}</a>
      </li>
    {{ end }}
  {{ end }}
</ul>

onedrawingperday’s answer is for this:

This is exactly what I was looking for. Thank you all.

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