Hello - I’m looping through my site category taxonomies and I want to add an “active” css class if I’m currently in a Node that is in that category.
This is my working code without the check:
<ul id="nav">
{{ $currentNode := . }}
{{ range $name, $taxonomy := .Site.Taxonomies.categories }}
<li><a href="/categories/{{ $name | urlize }}">{{ $name }}</a></li>
{{ end }}
</ul>
What I’m trying to accomplish is if $currentNode.Title === $name
. I tried doing the following but then my partial wouldn’t render anymore:
<ul id="navigation">
{{ $currentNode := . }}
{{ range $name, $taxonomy := .Site.Taxonomies.categories }}
{{ if .IsNode }}
{{ if in $currentNode.Title $name }}FOUND!{{end}}
{{ end }}
<li><a href="/categories/{{ $name | urlize }}">{{ $name }}</a></li>
{{ end }}
</ul>
Any ideas please!
bep
2
Have a look at the eq
template func.
Heh, I swear I’m blind sometimes. I looked at that doc 2 times! Can you tell me why .IsNode
doesn’t work in the range? Is it a scoping issue?
For instance, this does NOT work. The template is not outputted:
<ul id="navigation">
{{ $currentNode := . }}
{{ range $name, $taxonomy := .Site.Taxonomies.categories }}
{{ if .IsNode }}
{{ if eq $currentNode.Title $name }}FOUND!{{end}}
{{ end }}
<li><a href="/categories/{{ $name | urlize }}">{{ $name }}</a></li>
{{ end }}
</ul>
For instance, this DOES work. BUT I cannot check if the page is a node:
<ul id="navigation">
{{ $currentNode := . }}
{{ range $name, $taxonomy := .Site.Taxonomies.categories }}
{{ if eq $currentNode.Title $name }}FOUND!{{end}}
<li><a href="/categories/{{ $name | urlize }}">{{ $name }}</a></li>
{{ end }}
</ul>
bep
4
Yes, the “.” is now a category.
{{ if $.IsNode }}
Should be correct in most cases – but, of course, not when you are doing a range over a list pof pages.
Oh. Where does the idea of the .
being the current scope come from? Is that a GoLang thing or Hugo?
bep
6
That is Rob Pike in Google’s construction.