Can't access Site variables inside nested partial

Hi !

I got an issue recently and I can’t resolve it on my own. It might be really simple to solve it but everything I tried failed.

I have a nested partial, and I would like to access my Site variables inside of it, with no success. I read this part of the doc but it doesn’t seem to work in my case : https://gohugo.io/templates/introduction/#context-aka-the-dot

Here what my files looks like :

First I’ve got my index.html :

<!-- index.html -->

{{ partial "meta.html" . }}
    {{ partial "header.html" . }}

    <div class="container">
      <!-- Products Section -->
      {{ partial "sections/products.html" . }}
    </div>

    {{ partial "footer.html" . }}

Inside my sections/products.html partial, I got this :

<!-- products.html -->

  <section class="row main-container">
    <article class="accordion-container col-12">
      <div id="accordion" role="tablist">
        <!-- For multilangage purpose... -->
        {{ $data := (index $.Site.Data .Lang)}}
        {{ $featureData := $data.features}}
        
        <!-- here I loop over every files inside my data/features folder-->
        {{ range $featureData }}
          {{ partial "sections/feature.html" . }}
        {{end}} 

      </div> <!-- end of main accordion -->
    </article>
  </section>

and then inside my sections/feature.html, I can’t access any $.Site variable.

<!-- feature.html -->

{{ printf "%#v" $.Site }} <!-- return <nil> ->

When I debug it with printf, it tells me that the value is nil. I’m pretty sure this is a context issue, but I can’t understand what I did wrong. I put a dot . after each of my partials, as described in the doc to avoid this issue.

If you have any idea of what might be the problem it will be very nice !

Thank you :grin:

1 Like

Is that exactly what you have within this partial?

If yes, I don’t think that you can use .Site on its own. You need to specify what you need.

See:

Thanks for your answer.

In fact it was just an exemple, in reality I want to access the .Site.Data variable, exactly like in my products.html partial :

<!-- I got en/fr folders inside my data folder --> 
<!-- so this line is necessary in order to get the correct data according to the current language -->
{{ $data := (index $.Site.Data .Lang)}}

  <!-- then access index.yml (which reside inside my data folder) variables -->

{{ $data.index.someVariable }}

What is .index? That is not a Hugo variable.

Anyway I’m afraid that your question is a bit vague.

If you can please share your Hugo project so that we can have a better look.

index refers to my index.yml file, which resides into my data folder.

Sorry for my explanation, my original question was : why can I access to $.Site.Data inside my products.html partial, but not inside my feature.html partial (which resides into the products.html partial) ?

When I try to access $.Site.Data into my feature.html partial, my hugo build failed with this error :

error calling partial: template: partials/sections/feature.html:3:13:

executing "partials/sections/feature.html" at <index $.Site.Data .L...>: error calling index: index of untyped nil

(sorry I can’t share this project as it is for a real client :grimacing: )

Thanks for your help anyway !

It’s really difficult to troubleshoot an instance without running it locally. I suggest you break out the templates in question into a sample repo, and share that. Many times, in doing so, folks actually figure out their issue, because the reduced complexity illuminates the issue. For your consideration. :slight_smile:

You need to pass multiple values to your feature partial, but partials only take a single context parameter. To cram multiple values into a single value, you can build a dictionary on the fly.

In sections/products.html:

{{ partial "sections/feature.html" (dict "feature" . "ctx" $) }}

Then in sections/feature.html:

{{ .feature }}
{{ .ctx.Site }}
6 Likes

Yes!

That was the answer I was looking for! I didn’t know the existence of dict, but now everything makes perfect sense. I managed to succeed what I wanted to do, I only needed to access the Site variables from the nested partial.

Thank you again everybody ! :smile:

I know I’m commenting on a stale post, but I had a similar problem and ended up here.

What fixed it for me was replacing $.Site.Whatever with site.Whatever (as per https://gohugo.io/variables/site/#get-the-site-object-from-a-partial).

Cheers.

<aside> please someone let me know if there’s a better place to share this </aside>

6 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.