Hi I am trying to determine if i am on the current page so that i can set an active class on the css for that page. In my template I have this…
{{ $currentPage := . }}
{{ if eq $currentPage.Title .Name }}
<li class="uk-active"><a href={{ .URL }}>{{ .Name }}</a></li>
{{ else }}
<li class="uk-parent"><a href={{ .URL }}>{{ .Name }}</a></li>
{{ end }}
I then went into the _index.md file for the listpage view and added Title: “Projects” and still it doesn’t work. It works great for links that are just simple pages but just not the list view pages. Any ideas?
You have to use the preformatted text block (</> icon) for code, otherwise things will appear to be missing from your code. You might need something like the following, but you need to post more details about your theme / code. There’s no way to debug with that little information.
…
{{ $currentPage := . }}
…
{{ if eq $currentPage.Name .Name }}
<a href="{{ .URL }}" class="active">{{ .Name }}</a>
{{ else }}
<a href="{{ .URL }}">{{ .Name }}</a>
{{ end }}
What is the .Name
value for “Projects”?
It needs to match the case exactly for the condition to work.
Instead of checking for the .Name
variable try the following:
if eq $currentPage.RelPermalink .URL
The above will check whether the .RelPermalink
value of the $currentpage
equals the .URL
of a given menu item.
Then make sure to define the URL
value to: url = "/projects/"
If you still cannot get it to work, then as noted above, please share a sample repo, for us to have a look.