While working on the navigation for a documentation theme, I may have found a glitch in the function HasMenuCurrent. It generates the class .has-active in my templates. This class also appears on list pages with the class .is-active (generated by IsMenuCurrent), where it should not be, as the documentation states, if I understand correctly.
The source for documentation is available here: https://github.com/bowman2001/perplexdoc. I’ve managed to correct the glitch with a CSS workaround and the navigation bar is working as it is supposed to. But you can see is-active and has-active together in the code of the published site https://perplex.desider.at/doc.
Yes, but I haven’t expected both functions to return true for the same page. For a given menu, either the page is the current one (.IsMenuCurrent) or it is its parent (or parents parent …) (.HasMenuCurrent). Maybe I don’t understand the whole purpose of these functions, but this is how I understand the docs.
I just did some experimentation, and IsMenuCurrent works for me as documented, but HasMenuCurrent does not.
If I’m on a page /foo/ and that’s a list page with posts under it, HasMenuCurrent is true even though it’s the page directly linked by the menu item, contrary to the documentation. If /foo/ is a regular page (not a list page) then HasMenuCurrent is false (while IsMenuCurrent is true as expected).
{{- if .Site.Menus.nav }}
<nav>
{{ $currentPage := . -}}
{{- range .Site.Menus.nav -}}
<a class="{{ if or ($currentPage.IsMenuCurrent "nav" .) ($currentPage.HasMenuCurrent "nav" .) }}active{{end}}" href="{{ .URL }}">
{{- .Name -}}
</a>
{{ end -}}
</nav>
{{ end }}
This behaves as I intended because I’m using both IsMenuCurrent and HasMenuCurrent interchangeably and I wanted that behavior. But, in this setup, if I’m on /posts/ then both IsMenuCurrent and HasMenuCurrent are true. On /stuff/, which is not a list page, only IsMenuCurrent is true.
Thanks for the tip. I removed all identifiers for single pages. But my navigation still needs explicitly set identifiers for list pages, which are menu parents. They create unique checkboxes for opening the sub-menues.
If the variable .Identifier would be auto-generated from the .Name in case there is no identifier given in the parameters, I could omit most of them, too.