# Access root context($) from within a partial

**URL:** <https://discourse.gohugo.io/t/access-root-context-from-within-a-partial/15779>\
**Category:** support\
**Created:** [December 12, 2018, 4:20am UTC](https://discourse.gohugo.io/t/access-root-context-from-within-a-partial/15779 "2018-12-12T04:20:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![UtkarshVerma](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/utkarshverma/32/11273_2.png) [@UtkarshVerma](https://discourse.gohugo.io/u/UtkarshVerma)\
**Post date:** [December 12, 2018, 4:20am UTC](https://discourse.gohugo.io/t/access-root-context-from-within-a-partial/15779/1 "2018-12-12T04:20:56Z")

</div>

I was testing something. What I’ve been trying to do is to access the root context(`$`) from within a partial called as such:

```go
{{ partial "test.html" (dict "someVar" someVal "context" . )}}

```

When I do this, I am unable to access global variables such as $.Site.(something). I also tried passing a new variable, `"root" \$ ` but it fails. Please help me with this.

> Had to place a `\` beside $ because this site won’t render $ alone.

---

<div class="post-metadata">

**Author:** ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)\
**Post date:** [December 12, 2018, 4:39am UTC](https://discourse.gohugo.io/t/access-root-context-from-within-a-partial/15779/2 "2018-12-12T04:39:27Z")

</div>

The code you use to call your partial is fine.

You just need to access the context a bit differently. For example, to get the `baseURL` within your partial:

```auto
{{ .context.Site.BaseURL }}

```

---

<div class="post-metadata">

**Author:** ![UtkarshVerma](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/utkarshverma/32/11273_2.png) [@UtkarshVerma](https://discourse.gohugo.io/u/UtkarshVerma)\
**Post date:** [December 12, 2018, 4:45am UTC](https://discourse.gohugo.io/t/access-root-context-from-within-a-partial/15779/3 "2018-12-12T04:45:30Z")

</div>

Thanks for the quick reply. I tested it and it failed while I was trying to use:

```auto
{{ $paginator := .context.Paginate (where .Data.Pages "Type" .context.Site.Params.contentType) $itemsPerPage }}

```

Apparently, `$.Site.Params.contentType` used to work but `.context.Site.Params.contentType` returns nil.

---

<div class="post-metadata">

**Author:** ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)\
**Post date:** [December 12, 2018, 4:50am UTC](https://discourse.gohugo.io/t/access-root-context-from-within-a-partial/15779/4 "2018-12-12T04:50:16Z")

</div>

Hmm, I didn’t initially test this with params, but I am seeing the same thing now.

As a workaround, you can pass the param you need as part of the dictionary, e.g.

```auto
{{ partial "test.html" (dict "someVar" someVal "context" . "contentType" .Site.Params.contentType) }}

```

---

<div class="post-metadata">

**Author:** ![UtkarshVerma](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/utkarshverma/32/11273_2.png) [@UtkarshVerma](https://discourse.gohugo.io/u/UtkarshVerma)\
**Post date:** [December 12, 2018, 4:51am UTC](https://discourse.gohugo.io/t/access-root-context-from-within-a-partial/15779/5 "2018-12-12T04:51:30Z")

</div>

I am aware of that but there are a lot of variables used. So, I want to avoid this approach for tidyness.

---

<div class="post-metadata">

**Author:** ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)\
**Post date:** [December 12, 2018, 4:54am UTC](https://discourse.gohugo.io/t/access-root-context-from-within-a-partial/15779/6 "2018-12-12T04:54:04Z")

</div>

I see. In that case, just pass `.Site.Params`, then use as needed

```auto
{{ partial "test" (dict "params" .Site.Params) }}

```

---

<div class="post-metadata">

**Author:** ![UtkarshVerma](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/utkarshverma/32/11273_2.png) [@UtkarshVerma](https://discourse.gohugo.io/u/UtkarshVerma)\
**Post date:** [December 12, 2018, 4:59am UTC](https://discourse.gohugo.io/t/access-root-context-from-within-a-partial/15779/7 "2018-12-12T04:59:28Z")

</div>

In the meantime, I’ve also got stuck in another problem.  
I can’t access `.IsHome` in the partial using `.context.IsHome`. Hugo print the following in the console: `parse failed: template: partials/nav.html:5: function "context" not defined`

---

<div class="post-metadata">

**Author:** ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)\
**Post date:** [December 12, 2018, 5:03am UTC](https://discourse.gohugo.io/t/access-root-context-from-within-a-partial/15779/8 "2018-12-12T05:03:01Z")

</div>

That works for me. Maybe you are not passing the context?

Here’s my partial call:

```nohighlight
{{ partial "test" (dict "context" . "params" .Site.Params) }}

```

And here’s my test partial:

```nohighlight
{{ range $key, $value := .params }}
  {{ $key }} = {{ $value }}
{{ end }}

IsHome = {{ .context.IsHome }}

```

---

<div class="post-metadata">

**Author:** ![UtkarshVerma](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/utkarshverma/32/11273_2.png) [@UtkarshVerma](https://discourse.gohugo.io/u/UtkarshVerma)\
**Post date:** [December 12, 2018, 5:36am UTC](https://discourse.gohugo.io/t/access-root-context-from-within-a-partial/15779/9 "2018-12-12T05:36:22Z")

</div>

I’ve done exactly the same thing:

```auto
{{ partial "nav.html" (dict "context" . "rssButton" true "type" "") }}

```

This is inside the partial:

```auto
{{ if not .context.IsHome }}
	<div class="item">
              <a href="/">
		    <i class="inverted big link home icon" title="{{ i18n `home` }}"></i>
		</a>
	</div>
{{ end }}

```

But somehow, it still fails, giving the above stated message while building.  
This is the exact message:

```auto
executing "partials/nav.html" at <.context.IsHome>: can't evaluate field context in type *hugolib.PageOutput

```

---

<div class="post-metadata">

**Author:** ![UtkarshVerma](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/utkarshverma/32/11273_2.png) [@UtkarshVerma](https://discourse.gohugo.io/u/UtkarshVerma)\
**Post date:** [December 12, 2018, 5:54am UTC](https://discourse.gohugo.io/t/access-root-context-from-within-a-partial/15779/10 "2018-12-12T05:54:55Z")

</div>

Fixed it, I didn’t take in account the other partial calls, which were causing this build fail.
