List all pages by categories in hugo

Viewed 559

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
2 Answers

layouts/partials/walk.html:

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

content/section/_index.md:

---
title: Section Name
---

layouts/index.html

{{ partial "walk.html" }}

In the end to format the data you are still using HTML, so the rules for nested list still apply, for example:

<ul>
    {{ range .OuterList }}
        <li>
            <a href="#">{{ .OuterListItem }}</a>
            {{if gt (len .OuterListItem.InnerItems) 0}}
            <ul>
               {{ range .OuterListItem.InnerItems }}
                  <li> {{ .InnerItem }} </li>
               {{ end }}
            </ul>
            {{ end }}
        </li>
    {{ end }}
</ul>
Related