Generate menu from mix of Sections and Pages

Can I take content like this:

content
├── _index.md   ← "Home"
├── about.md
├── contact.md
└── fruits
    ├── apple.md
    └── orange.md

And generate (adaptively) a menu:

Home | About | Contact | Fruits

… that knows which page it’s currently being generated on?

If that’s not possible adaptively, which approach would you suggest? Defining the menu in the global config? Would I still be able to figure out the current page?

I know I can also define the menu separately on each page, but that seems like error-prone duplication I’d prefer to avoid.

My suggestion would be to avoid the use of menus altogether, if you want them to be dynamic and instead use a combination of {{.Site.Sections}} and {{.Site.Home.RegularPages}} when you build your navigation links in your template. However, if you want to still use Menus for your Sections, then you can access the .Page variable to then list the pages for that Section:

<nav>
{{if .IsHome}}
<ul id="homepages">
{{range .Site.Home.RegularPages}}
<li><a href="{{.RelPermalink}}">{{.Title}}</a></li>
{{end}}
</ul>
<ul id="homesections">
{{range .Site.Sections}}
<li><a href="{{.RelPermalink}}">{{.Title}}</a></li>
{{end}}
</ul>
{{end}}
{{if not .IsHome}}
<ul id="pages">
{{range .RegularPages}}
<li><a href="{{.RelPermalink}}">{{.Title}}</a></li>
{{end}}
</ul>
<ul id="sections">
{{range .Sections}}
<li><a href="{{.RelPermalink}}">{{.Title}}</a></li>
{{end}}
</ul>
{{end}}
</nav>
1 Like