Hi all, I’m building out a documentation site which has a Main nav and Sidebar nav. After researching I’ve landed on building out my menu using the config file. The menus render correctly, but I’m stuck on how to render my sidebar navigation based on the section I’m in using the config file (see example below).
Here’s my config json. As you can see I’m calling out a single “main” menu, but using “parent” for navigation that would fall under the sidebar nav.
{
"baseURL": "http://example.com/",
"languageCode": "en-us",
"title": "DS",
"menu": {
"main": [
{
"name": "Getting Started",
"url": "/getting-started",
"pageRef": "getting-started",
"weight": 10
},
{
"name": "Contents",
"url": "/contents",
"pageRef": "contents",
"parent": "Getting Started"
},
{
"name": "Download",
"url": "/download",
"pageRef": "download",
"parent": "Getting Started"
},
{
"name": "Foundations",
"url": "/foundations",
"pageRef": "foundations",
"weight": 20
},
{
"name": "Colors",
"url": "/colors",
"pageRef": "colors",
"parent": "Foundations"
},
{
"name": "Grid",
"url": "/grid",
"pageRef": "grid",
"parent": "Foundations"
},
{
"name": "Components",
"url": "/components",
"pageRef": "components",
"weight": 30
},
{
"name": "Alerts",
"url": "/alerts",
"pageRef": "alerts",
"parent": "Components"
},
{
"name": "Badges",
"url": "/badges",
"pageRef": "badges",
"parent": "Components"
},
{
"name": "Utilities",
"url": "/utilities",
"pageRef": "utilities",
"weight": 40
},
{
"name": "Background",
"url": "/background",
"pageRef": "background",
"parent": "Utilities"
},
{
"name": "Borders",
"url": "/borders",
"pageRef": "borders",
"parent": "Utilities"
}
]
}
}
Here’s my code for the Main nav which works great.
<header class="header">
<nav class="header__nav">
<div class="container header__nav-container">
<div class="header__brand">
<a href="{{ .Site.BaseURL }}" class="header__brand-type-link">{{ .Site.Title }}</a>
</div>
<ul class="header__nav-list">
{{- $currentPage := . -}}
{{- range .Site.Menus.main -}}
<li class="header__nav-item">
<a href="{{ .URL }}" class='header__nav-link {{ if or ($currentPage.IsMenuCurrent "main" .) ($currentPage.HasMenuCurrent "main" .) }}current_page_link{{end}}'>{{ .Name }}</a>
</li>
{{ end }}
</ul>
</div>
</nav>
</header>
Here’s the code for my Sidebar nav. This would work great if I didn’t need the navigation to display based on the section you’re in. I have a feeling it has something to to with the .Children in my range. I tried methods like .Site.Section.Children, but that didn’t work. Any thoughts on what I’m missing here?
<nav class="sidebar__nav">
<ul class="sidebar__nav-list">
{{- $currentPage := . -}}
{{- range .Site.Menus.main -}}
{{- if .HasChildren -}}
{{- range .Children -}}
<li class="sidebar__nav-item">
<a href="{{ .URL }}" class='sidebar__nav-link {{ if $currentPage.IsMenuCurrent "main" . }}current_page_link{{end}}'>{{ .Name }}</a>
</li>
{{ end }}
{{ end }}
{{ end }}
</ul>
</nav>