Hi all,
On my site, I have the main navigation menu that lists all the sections, such as:
Blog (www.domain.com/blog)
About (www.domain.com/about)
CV (www.domain.com/cv)
The following code correctly marks a link as active:
{{- define "section-tree-nav" }}
{{- $currentNode := .currentnode }}
{{- with .sect}}
{{- if and .IsSection (or (not .Params.hidden) $.showhidden)}}
{{- $numberOfPages := (add (len .Pages) (len .Sections)) }}
{{- safeHTML .Params.head}}
<li data-nav-id="{{.RelPermalink}}" class="nav-item my-1
{{- if .IsAncestor $currentNode}} parent{{end}}
{{- if eq .RelPermalink $currentNode.RelPermalink}} active{{end}}
{{- if .Params.alwaysopen}} alwaysopen{{end -}}
{{- if ne $numberOfPages 0 }} haschildren{{end}}
">
<!-- I'm the params head -->
<a class="nav-link p-0" href="{{.RelPermalink}}" {{- if eq "Skills Reference" .Params.Title}}target="_blank" {{end}}><h6>{{safeHTML .Params.Pre}}{{.Title}}{{safeHTML .Params.Post}}{{- if eq "Skills Reference" .Params.Title}}<i id="skills-ref-external-icon" class="fas fa-external-link-alt"></i>{{end}}</h6>
</a>
{{- if and (ne $numberOfPages 0) (ne "Skills Reference" .Params.Title) }}
<ul class="list-unstyled ml-2">
{{- .Scratch.Set "pages" .Pages }}
{{- if .Sections}}
{{- .Scratch.Set "pages" (.Pages | union .Sections) }}
{{- end}}
{{- $pages := (.Scratch.Get "pages") }}
{{- if eq .Site.Params.ordersectionsby "title"}}
{{- range $pages.ByTitle }}
{{- if and .Params.hidden (not $.showhidden) }}
{{- else}}
{{- template "section-tree-nav" dict "sect" . "currentnode" $currentNode }}
{{- end}}
{{- end}}
{{- else}}
{{- range $pages.ByWeight }}
{{- if and .Params.hidden (not $.showhidden) }}
{{- else}}
{{- template "section-tree-nav" dict "sect" . "currentnode" $currentNode }}
{{- end}}
{{- end}}
{{- end}}
</ul>
{{- end}}
</li>
{{- if eq "Skills Reference" .Params.next}}
<hr class="solid-nav-separator">
{{- end }}
{{- else}}
{{- if not .Params.Hidden }}
<li data-nav-id="{{.RelPermalink}}" class="nav-item my-1 {{- if eq .RelPermalink $currentNode.RelPermalink}} active{{end -}}">
<!-- I'm the params hidden -->
<a href="{{.RelPermalink}}" class="nav-link p-0">
{{safeHTML .Params.Pre}}{{.LinkTitle}}{{safeHTML .Params.Post}}
</a>
</li>
{{- end}}
{{- end}}
{{- end}}
{{- end}}
For example, if you’re at www.domain.com/about/
, the about
link in the sidebar is correctly set to active
with the code above.
I override this menu if we’re on a specific page. For example, if you’re on www.domain.com/blog/
, the menu is overridden to list the child pages, such as blog/post1
, blog/post2
, etc so the sidebar now looks like:
Post 1 (www.domain.com/blog/post1/)
Post 2 (www.domain.com/blog/post2/)
Now, if a user clicks Post 1 in the sidebar, it should also be marked as active
, but the following code doesn’t seem to do that:
{{define "skills-ref-menu"}}
{{- $currentNode := . }}
{{- range .menu}}
<li data-nav-id="{{.RelPermalink}}" class="nav-item my-1 {{- if eq .RelPermalink $currentNode.RelPermalink}} active{{end}}
">
<a class="nav-link p-0" href="{{.RelPermalink}}"><h6>{{safeHTML .Params.Pre}}{{.Title}}{{safeHTML .Params.Post}}</h6></a>
</li>
{{- end }}
{{- end }}
From what I can tell, $currentNode.RelPermalink
is nil
, so {{- if eq .RelPermalink $currentNode.RelPermalink}} active{{end}}
is never satisfied. Is there a different way to do this? Can I somehow compare the .RelPermalink
to everything after the hostname in the URL (e.g. www.domain.com/blog/post1/
→ /blog/post1
)?