# Can't access Site variables inside nested partial

**URL:** <https://discourse.gohugo.io/t/cant-access-site-variables-inside-nested-partial/12437>\
**Category:** support\
**Created:** [June 15, 2018, 10:03am UTC](https://discourse.gohugo.io/t/cant-access-site-variables-inside-nested-partial/12437 "2018-06-15T10:03:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![antoine1000](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/antoine1000/32/7715_2.png) [@antoine1000](https://discourse.gohugo.io/u/antoine1000)\
**Post date:** [June 15, 2018, 10:03am UTC](https://discourse.gohugo.io/t/cant-access-site-variables-inside-nested-partial/12437/1 "2018-06-15T10:03:28Z")

</div>

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](https://gohugo.io/templates/introduction/#context-aka-the-dot)

Here what my files looks like :

First I’ve got my index.html :

```auto
<!-- 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 :

```auto
<!-- 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.

```auto
<!-- 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 😁

---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [June 15, 2018, 10:40am UTC](https://discourse.gohugo.io/t/cant-access-site-variables-inside-nested-partial/12437/2 "2018-06-15T10:40:56Z")

</div>

> [@antoine1000](#):
>
> `{{ printf "%#v" $.Site }} <!-- return <nil> ->`

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:

> **[Site variables](https://gohugo.io/variables/site/#readout)**
>
> Use these methods with Site objects. A multilingual project will have two or more sites, one for each language.

---

<div class="post-metadata">

**Author:** ![antoine1000](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/antoine1000/32/7715_2.png) [@antoine1000](https://discourse.gohugo.io/u/antoine1000)\
**Post date:** [June 15, 2018, 11:18am UTC](https://discourse.gohugo.io/t/cant-access-site-variables-inside-nested-partial/12437/3 "2018-06-15T11:18:43Z")

</div>

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 :

```auto
<!-- 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 }}

```

---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [June 15, 2018, 11:35am UTC](https://discourse.gohugo.io/t/cant-access-site-variables-inside-nested-partial/12437/4 "2018-06-15T11:35:38Z")

</div>

> [@antoine1000](#):
>
> {{ $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.

---

<div class="post-metadata">

**Author:** ![antoine1000](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/antoine1000/32/7715_2.png) [@antoine1000](https://discourse.gohugo.io/u/antoine1000)\
**Post date:** [June 15, 2018, 11:49am UTC](https://discourse.gohugo.io/t/cant-access-site-variables-inside-nested-partial/12437/5 "2018-06-15T11:49:13Z")

</div>

`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 😬 )

Thanks for your help anyway !

---

<div class="post-metadata">

**Author:** ![maiki](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/maiki/32/16384_2.png) [@maiki](https://discourse.gohugo.io/u/maiki)\
**Post date:** [June 15, 2018, 7:12pm UTC](https://discourse.gohugo.io/t/cant-access-site-variables-inside-nested-partial/12437/6 "2018-06-15T19:12:06Z")

</div>

> [@antoine1000](#):
>
> (sorry I can’t share this project as it is for a real client 😬 )

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. 🙂

---

<div class="post-metadata">

**Author:** ![moorereason](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/moorereason/32/592_2.png) [@moorereason](https://discourse.gohugo.io/u/moorereason)\
**Post date:** [June 15, 2018, 8:27pm UTC](https://discourse.gohugo.io/t/cant-access-site-variables-inside-nested-partial/12437/7 "2018-06-15T20:27:06Z")

</div>

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`:

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

```

Then in `sections/feature.html`:

```auto
{{ .feature }}
{{ .ctx.Site }}

```

---

<div class="post-metadata">

**Author:** ![antoine1000](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/antoine1000/32/7715_2.png) [@antoine1000](https://discourse.gohugo.io/u/antoine1000)\
**Post date:** [June 15, 2018, 10:15pm UTC](https://discourse.gohugo.io/t/cant-access-site-variables-inside-nested-partial/12437/8 "2018-06-15T22:15:01Z")

</div>

> [@moorereason](#):
>
> {{ partial “sections/feature.html” (dict “feature” . “ctx” $) }}

**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 ! 😄

---

<div class="post-metadata">

**Author:** ![benerik](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/benerik/32/11707_2.png) [@benerik](https://discourse.gohugo.io/u/benerik)\
**Post date:** [June 27, 2020, 1:50am UTC](https://discourse.gohugo.io/t/cant-access-site-variables-inside-nested-partial/12437/9 "2020-06-27T01:50:35Z")

</div>

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](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\>

---

<div class="post-metadata">

**Author:** ![maiki](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/maiki/32/16384_2.png) [@maiki](https://discourse.gohugo.io/u/maiki)\
**Post date:** [June 28, 2020, 1:50am UTC](https://discourse.gohugo.io/t/cant-access-site-variables-inside-nested-partial/12437/10 "2020-06-28T01:50:41Z")

</div>

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