.Site.Menu.main empty inside partial template

Hi everyone.

I am new in Hugo and love it.

I have a problem with showing the menu in the template. I have for example in my root config.toml this snippet:

[[menu.main]]
name = "Contact"
url = "/contact/"
weight = 1

[[menu.main]]
name = "Project"
url = "/project/"
weight = 2

Then in the template when I try to get the menu the variable .Site.Menu.main is empty.

I use this variable in a partial called nav.html that i just want to hold my menu.

Does anyone have any idea how to show the menu?
Thank you.

My best guess is that you forgot to pass the page (the “.”) as an argument to the partial.

Well in my index.html I passed the argument for the partial like this

{{ partial “nav.html” . }}

Is this what you mean?

Thank you very much.

Probably, it is hard to judge without seeing the complete template/project.

Yes of course and thank you @bep for your time.

This is the layouts/index.html

<!DOCTYPE html>
<html lang="en">

  {{ partial "head.html" . }}

  <body>

    <div id="all">

        <header>

            {{ partial "language_switcher.html" . }}

            {{ partial "nav.html" . }}

        </header>

        {{ partial "carousel.html" . }}

        {{ partial "objectives.html" . }}

        {{ partial "see_more.html" . }}

        {{ partial "features.html" . }}

        {{ partial "latest_news.html" . }}

        {{ partial "clients.html" . }}

        {{ partial "footer.html" . }}

    </div>
    <!-- /#all -->

    {{ partial "scripts.html" . }}

  </body>
</html>

Then for layouts/partials/nav.html is this:

<div class="navbar-affixed-top" data-spy="affix" data-offset-top="200">

    <div class="navbar navbar-default yamm" role="navigation" id="navbar">

        <div class="container">
            <div class="navbar-header">
                <a class="navbar-brand home" href="{{ .Site.BaseURL }}">
                    <img class="logo-box" src="{{ .Site.BaseURL }}img/Logo_Img.png"  style="height: 50px;" alt="{{ .Title }} logo">
                    <span class="sr-only">{{ .Title }} - go to homepage</span>
                </a>
                <div class="navbar-buttons">
                    <button type="button" class="navbar-toggle btn-template-main" data-toggle="collapse" data-target="#navigation">
                        <span class="sr-only">Toggle navigation</span>
                        <i class="fa fa-align-justify"></i>
                    </button>
                </div>
            </div>
            <!--/.navbar-header -->

            <div class="navbar-collapse collapse" id="navigation">
                <ul class="nav navbar-nav navbar-right">
                    {{ range .Site.Menus.main }}
                        <li><a href="{{ .URL }}">{{ .Name }}</a></li>
                    {{ end }}
                </ul>
            </div>
            <!--/.nav-collapse -->

        </div>
    </div>
    <!-- /#navbar -->

</div>

<!-- *** NAVBAR END ***-->

And finally the config.toml is this

...

[[menu.main]]
    name = "Contact"
    url  = "/contact/"
    weight = 1

[[menu.main]]
    name = "Project"
    url  = "/project/"
    weight = 2

...

Do you need more files to provide to you?

Any help would be grateful.
Thank you

It is a little bit hard to see without the full project, but the templates look correct, so I’m guessing the problem is in your config.toml file. Not sure what that can be (you posted only a small part of it), but one tip would be to make sure that all the map definitions is placed at the end.

This is all the config.toml

baseurl = "https://www.....eu/"

theme = "hugo-universal-theme"
languageCode = "en-us"

# Enable comments by entering your Disqus shortname
disqusShortname = ""

# Enable Google Analytics by entering your tracking code
googleAnalytics = ""

# Define the number of posts per page
paginate = 10

DefaultContentLanguage = "en"

[Languages]
[Languages.en]
#English
title = "example"

[Languages.de]
#German
title = "example"

[Languages.el]
#Greek
title = "example"

[Languages.nl]
#Dutch
title = "example"

[Languages.sk]
#Slovak
title = "example"

[Languages.sl]
#Slovenian
title = "example"

[Languages.es]
#Spanish
title = "example"

[params]

    # Style options: default (light-blue), blue, green, marsala, pink, red, turquoise, violet
    style = "red"




[params.carousel]
    enable = true
    # All carousel items are defined in their own files. You can find example items
    # at 'exampleSite/data/carousel'.
    # For more informtion take a look at the README.

[params.see_more]
    enable = true
    icon = "fa fa-file-code-o"
    title = "Visit example Toolbox online Platform"
    link_url = "#"
    link_text = "Go to platform"

[params.features]
    enable = true
    # All features are defined in their own files. You can find example items
    # at 'exampleSite/data/features'.
    # For more informtion take a look at the README.

[params.latest_news]
    enable = true
    title = "Latest News"

[params.clients]
    enable = true
    # All clients are defined in their own files. You can find example items
    # at 'exampleSite/data/clients'.
    # For more informtion take a look at the README.
    title = "Project Partners"

# Enable and disable widgets for the right sidebar
[params.widgets]
    categories = true
    tags = true
    search = true




[[menu.main]]
    name = "Contact"
    url  = "/contact/"
    weight = 1

[[menu.main]]
    name = "Project"
    url  = "/project/"
    weight = 2

@bep do you think I should change the order? I did that but nothing changed

I rewrote the nav.html and now it seems to work. I don’t know what was wrong. Maybe a typo.

Thank you anyway. I miss knowledge and that makes things hard for me.

Now I am looking how to implement a language switcher. If you have any guidance to provide me please do. It will save me a lot of time.

Thank you

So weird, I’m also having the exact same problem, nothing shows up when the code is in a partial, but the exact same code works if I move to in index.html

Did you ever figure out what the issue was?

Just learning about Hugo so I’m probably missing something.

Oh also I’m using Hugo v0.19 not sure it makes a difference?

Ok it turns out I was not passing the . to the partial, which is the context.

Glad to here you worked it out.

In general I would suggest you to make use of debugging https://gohugo.io/templates/debugging/ .

It will help you understand how to reference map variables.

tio, this is helpful.
See the last code block on the debugging page URL you provided.

{{ partial “header” . }}
Notice the period after “header”