Can't get .CurrentPage

Hello. I am creating a menu using hugo. I want to add myself an indication of the current page. To do this, there is the following code:

{{- $currentPage := .CurrentPage -}}

{{ if .URL }}
      <a href="{{ .URL }}">
{{ end }}
{{ if $currentPage.IsMenuCurrent "main" . -}}
      <b>{{- .Name -}} 1</b>
 {{- else if $currentPage.HasMenuCurrent "main" . -}}
      <b>{{- .Name -}} 2</b>
 {{- else -}}
      {{- .Name -}} 3
 {{- end -}}
 {{ if .URL }}
      </a>
 {{ end }}

But it doesn’t work (executed branch if else with number 3). When I want to check variables:

{{ print $currentPage }}
{{ print .CurrentPage }}

I am getting nil. What have I done wrong?

There is no Page variable called .CurrentPage in Hugo.
Also I’m not sure how you don’t get a console ERROR that breaks the site.

You need to share the project (or a sample of the project with dummy content) along with a description of what you’re trying to do.

Also have a look at:

Are you trying to achieve something similar to this? hugo-community/navbar.html at main · chrisreddington/hugo-community (github.com)

I think you’re missing a variable assignment of something like {{ $currentPage := . }} and then you can use the HasMenuCurrent / IsMenuCurrent like in the sample I sent i believe.

2 Likes