# Template variable not defined

**URL:** <https://discourse.gohugo.io/t/template-variable-not-defined/40851>\
**Category:** support\
**Created:** [October 7, 2022, 9:46pm UTC](https://discourse.gohugo.io/t/template-variable-not-defined/40851 "2022-10-07T21:46:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rshepard](https://avatars.discourse-cdn.com/v4/letter/r/d26b3c/32.png) [@rshepard](https://discourse.gohugo.io/u/rshepard)\
**Post date:** [October 7, 2022, 9:46pm UTC](https://discourse.gohugo.io/t/template-variable-not-defined/40851/1 "2022-10-07T21:46:12Z")

</div>

I found a web site documenting how to create a menu with submenus and I copied the nav.html file from that site. There’s an error in nav.html. The file:

```auto
<nav class="nav">
    <ul class="menu">
        {{ range .Site.Menus.main }}
        <li class="{{ if $currentPage.HasMenuCurrent "main" . }}active{{ end }}">
            <span>{{ .Name }}</span>
            {{ if .HasChildren }}
            <span class="drop-icon" for="{{ .Name }}">▾</span>
            <ul class="sub-menu">
                {{ range .Children }}
                <li class="{{ if $currentPage.HasMenuCurrent "main" . }}active{{ end }}">
                    <span>{{ .Name }}</span>
                </li>
                {{ end }}
            </ul>
           {{ end }}
        </li>
        {{ end }}
    </ul>
</nav>

```

This throws an error when I try to invoke hugo server:

```auto
$ hugo server
Error: add site dependencies: load resources: loading templates:
"../layouts/partials/nav.html:4:1":
parse failed: template: partials/nav.html:4: undefined variable
"$currentPage"

```

Where do I define $currentPage so Hugo accepts the variable?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [October 7, 2022, 11:19pm UTC](https://discourse.gohugo.io/t/template-variable-not-defined/40851/2 "2022-10-07T23:19:51Z")

</div>

Look at the first example on this page:  
[https://gohugo.io/templates/menu-templates/](https://gohugo.io/templates/menu-templates/)

---

<div class="post-metadata">

**Author:** ![rshepard](https://avatars.discourse-cdn.com/v4/letter/r/d26b3c/32.png) [@rshepard](https://discourse.gohugo.io/u/rshepard)\
**Post date:** [October 8, 2022, 12:40am UTC](https://discourse.gohugo.io/t/template-variable-not-defined/40851/3 "2022-10-08T00:40:43Z")

</div>

Joe,

I’ve started reading all the Hugo template pages and hadn’t yet come to the  
menu page. This looks like it puts the menu on a sidebar rather than the top  
of each page, but it’s a good start for me.

Thanks,

Rich

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [October 8, 2022, 12:44am UTC](https://discourse.gohugo.io/t/template-variable-not-defined/40851/4 "2022-10-08T00:44:33Z")

</div>

The point of that example is to show where `$currentPage` is initialized.

---

<div class="post-metadata">

**Author:** ![tut](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@tut](https://discourse.gohugo.io/u/tut)\
**Post date:** [October 8, 2022, 5:43am UTC](https://discourse.gohugo.io/t/template-variable-not-defined/40851/5 "2022-10-08T05:43:56Z")

</div>

Try this

```auto
<nav class="nav">
    <ul class="menu">
      {{ $currentPage := . }}
        {{ range .Site.Menus.main }}
        <li class="{{ if $currentPage.HasMenuCurrent "main" . }}active{{ end }}">
            <span>{{ .Name }}</span>
            {{ if .HasChildren }}
            <span class="drop-icon" for="{{ .Name }}">▾</span>
            <ul class="sub-menu">
                {{ range .Children }}
                <li class="{{ if $currentPage.HasMenuCurrent "main" . }}active{{ end }}">
                    <span>{{ .Name }}</span>
                </li>
                {{ end }}
            </ul>
           {{ end }}
        </li>
        {{ end }}
    </ul>
</nav>

```

---

<div class="post-metadata">

**Author:** ![rshepard](https://avatars.discourse-cdn.com/v4/letter/r/d26b3c/32.png) [@rshepard](https://discourse.gohugo.io/u/rshepard)\
**Post date:** [October 8, 2022, 1:40pm UTC](https://discourse.gohugo.io/t/template-variable-not-defined/40851/6 "2022-10-08T13:40:29Z")

</div>

Tut,

Thank you very much. Another valuable lesson for a new Hugo user.

Much appreciated,

Rich

---

<div class="post-metadata">

**Author:** ![rshepard](https://avatars.discourse-cdn.com/v4/letter/r/d26b3c/32.png) [@rshepard](https://discourse.gohugo.io/u/rshepard)\
**Post date:** [October 8, 2022, 3:59pm UTC](https://discourse.gohugo.io/t/template-variable-not-defined/40851/7 "2022-10-08T15:59:39Z")

</div>

Thanks, Joe.

What threw me off is that the initialization was not included in the  
source’s web page.

Regards,

Rich
