Automatic nested menu listing all sections, subsections and pages

I’ve googled quite some time to find out how to “automatically” create a menu listing all sections, n-level subsections and pages (see Lazy blogger menu (weird suffix, nested)).

Sadly, the “lazy blogger menu” does not work for my case, since it lists only the top level sections and neither subsections nor pages.

So this is what I’ve come up with:
section-menu.html

{{ $page := .currentPage}}
{{ range .menu }}
    <li>
        <a href="{{ .URL }}" class="{{if .IsAncestor $page}}active{{end}}">{{ .Name }}</a></li>
    <ul>
        {{ if .Sections }}
            {{- partial "section-menu.html" (dict "menu" .Sections "currentPage" $page)}}
        {{ end }}
        {{ $sectionPages := where .Pages "File.Dir" "in" .Dir }}
        {{ range $sectionPages }}
            <li>
                <a href="{{ .URL }}" class="{{if eq . $page}}active{{end}}">{{ .Name }}</a></li>
        {{ end }}
    </ul>
{{ end }}

Use it like this for listing all sections of the site:

{{- partial "section-menu.html" (dict "menu" .Site.Sections "currentPage" .Page) -}}

Or like this to list only the subsections and subpages of the current section:

{{- partial "section-menu.html" (dict "menu" .CurrentSection.Sections "currentPage" .Page) -}}
1 Like

Thanks for the tip. I think you will also like this one