I have a site where I want to be able to visually display to the user indication about what page they are on by adding an active
class to the navigation element. I have this working (see my site), but it looks like a terrible hack. Surely there is a better way of doing this?
In my config.toml
, I declare my site-wide menus as following:
[menu]
[[menu.main]]
identifier = "news"
name = "News"
url = "/"
weight = 0
[[menu.main]]
identifier = "projects"
name = "Projects"
url = "/projects/"
weight = 1
[[menu.main]]
identifier = "portfolio"
name = "Portfolio"
url = "/portfolio/"
weight = 2
[[menu.main]]
identifier = "research"
name = "Research"
url = "/research/"
weight = 3
[[menu.main]]
identifier = "about"
name = "About"
url = "/about/"
weight = 4
Then in my Boostrap navbar partial I’m doing this to determine if the current menu item should be marked as active:
<div class="navbar-collapse collapse" id="navbar">
<ul class="nav navbar-nav navbar-right">
{{ range .Site.Menus.main }}
<li
{{ if $.IsNode }}{{ if eq .Identifier "news" }}class="active"{{ end }}{{ end }}
{{ if $.IsPage }}{{ if eq .Identifier $.Type }}class="active"{{ end }}{{ end }}
>
<a href="{{ .URL }}">{{ .Name }}</a>
</li>
{{ end }}
</ul>
</div>
Do I really have to treat nodes and pages differently like this? Is there any way I can check one single variable against the current menu to determine if it should be highlighted? I wouldn’t mind setting front matter on the pages, but there is no way to do this for list.html
based templates…