# Passing a variable to a partial, again

**URL:** <https://discourse.gohugo.io/t/passing-a-variable-to-a-partial-again/20729>\
**Category:** support\
**Created:** [September 11, 2019, 7:38am UTC](https://discourse.gohugo.io/t/passing-a-variable-to-a-partial-again/20729 "2019-09-11T07:38:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![juh2](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/juh2/32/3980_2.png) [@juh2](https://discourse.gohugo.io/u/juh2)\
**Post date:** [September 11, 2019, 7:38am UTC](https://discourse.gohugo.io/t/passing-a-variable-to-a-partial-again/20729/1 "2019-09-11T07:38:44Z")

</div>

I tried the solutions given in various posts on passing a variable to a partial. I might be blocked by some fundamental misunderstanding.

I want to call a partial from a partial.

```
{{ partial "banner-nav-box.html" (dict "bereich" "allgemein") }}

```

banner-nav-box.html:

```
<article>
  <a href="{{ with .Site.GetPage "section" "{{ .bereich }}" }}{{ .RelPermalink }}{{ end }}">
   {{ $preview := (site.GetPage "/{{ .bereich }}/_index.md").Resources.GetMatch "feature" }}
   {{ with $preview }} {{$scaled := .Fill "566x378 Gaussian Center" }}
    <img src="{{ $scaled.RelPermalink }}" class="" alt="{{ $scaled.Title }}">
   {{ end }}
  </a>
<div>
  {{ with .Site.GetPage "section" "{{ .bereich }}" }} {{ .Title }}
</div>
<p>
  {{ .Description }}
</p>
<p><a href="{{ .RelPermalink }}">Weiter</a></p>
  {{ end }}
</div>
</article>

```

The HTML output is empty where ever the variable .bereich is used.  
The code works if I insert “allgemein” by hand.

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [September 11, 2019, 8:08am UTC](https://discourse.gohugo.io/t/passing-a-variable-to-a-partial-again/20729/2 "2019-09-11T08:08:43Z")

</div>

call `{{ partial "banner-nav-box.html" bereich="allgemein" }}` with the following:

```auto
{{ $bereich := .Get "bereich" }}
{{ $path := delimit (slice "foo" "bar" "buzz") "" }}
<article>
  <a href="{{ with .Site.GetPage "section" "$bereich" }}{{ .RelPermalink }}{{ end }}">
   {{ $preview := (site.GetPage "$path").Resources.GetMatch "feature" }}
   {{ with $preview }} {{$scaled := .Fill "566x378 Gaussian Center" }}
    <img src="{{ $scaled.RelPermalink }}" class="" alt="{{ $scaled.Title }}">
   {{ end }}
  </a>
<div>
  {{ with .Site.GetPage "section" "$bereich" }} {{ .Title }}
</div>
<p>
  {{ .Description }}
</p>
<p><a href="{{ .RelPermalink }}">Weiter</a></p>
  {{ end }}
</div>
</article>

```

1. you can’t use brackets inside of bracket (`{{bla {{ .fasel}}}}` which is why you have to create variables outside (line 1 and 2)
2. this is untested, it could be that the hyphens around the variables in the calls (with, $preview aso.) have to be removed. I never get it right when they are needed and when not in these cases.

---

<div class="post-metadata">

**Author:** ![funkydan2](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/funkydan2/32/5636_2.png) [@funkydan2](https://discourse.gohugo.io/u/funkydan2)\
**Post date:** [September 11, 2019, 8:10am UTC](https://discourse.gohugo.io/t/passing-a-variable-to-a-partial-again/20729/3 "2019-09-11T08:10:53Z")

</div>

As @davidsneighbour said…and I also think it’s an issue with scoping (i.e. what happens when you call the `with` function). Have read of

> **[Introduction to Hugo Templating](https://gohugo.io/templates/introduction/#the-dot)**
>
> Hugo uses Go's \`html/template\` and \`text/template\` libraries as the basis for the templating.

---

<div class="post-metadata">

**Author:** ![juh2](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/juh2/32/3980_2.png) [@juh2](https://discourse.gohugo.io/u/juh2)\
**Post date:** [September 11, 2019, 8:21am UTC](https://discourse.gohugo.io/t/passing-a-variable-to-a-partial-again/20729/4 "2019-09-11T08:21:57Z")

</div>

Thanks a lot for your hints.

> [@davidsneighbour](#):
>
> {{ $path := delimit (slice “foo” “bar” “buzz”) “” }}

I have to use $bereich in the slice part:

```
{{$bereich := .bereich }}
{{ $path := delimit (slice "/" "$bereich" "_index.md") "" }}

```

which does not expand.

```
{{$bereich := .Get "bereich" }}

```

causes an error:

```
Get is not a method but has arguments

```

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [September 11, 2019, 8:32am UTC](https://discourse.gohugo.io/t/passing-a-variable-to-a-partial-again/20729/5 "2019-09-11T08:32:45Z")

</div>

What’s up with the `{{$bereich := .bereich }}`? That’s not the reason for the error message, but you are overwriting $bereich there with a non existing variable.

Instead of `{{ $bereich := .Get "bereich" }}` try `{{ $bereich := $.Params.bereich }}`, that seems to have the same effect (says the docs 😉 )

> **[Create Your Own Shortcodes](https://gohugo.io/templates/shortcode-templates/#access-parameters)**
>
> You can extend Hugo's built-in shortcodes by creating your own using the same templating syntax as that for single and list pages.

---

<div class="post-metadata">

**Author:** ![juh2](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/juh2/32/3980_2.png) [@juh2](https://discourse.gohugo.io/u/juh2)\
**Post date:** [September 11, 2019, 9:07am UTC](https://discourse.gohugo.io/t/passing-a-variable-to-a-partial-again/20729/6 "2019-09-11T09:07:41Z")

</div>

> [@davidsneighbour](#):
>
> What’s up with the `{{$bereich := .bereich }}` ?

It works in so far that I can call {{ $bereich }} somewhere in the partial getting the right value.

You point to a shortcode documentation, but I am working with a partial.

With $.Params.bereich I get empty code as well.

I modified my code to this, but I get empty html:

```
<div class="fl w-100 w-50-m w-20-l">
<article class="pa3 bg-washed-red b--white bw3">
	{{$bereich := .bereich }}
	{{ $path := delimit (slice "/" "$bereich" "/" "_index.md") "" }}
	<a href="{{ with .Site.GetPage "section" "$bereich" }}{{ .RelPermalink }}{{ end }}">
		{{ $preview := (site.GetPage "$path").Resources.GetMatch "feature" }}
		{{ with $preview }}
		{{ $scaled := .Fill "566x378 Gaussian Center" }}
		<img src="{{ $scaled.RelPermalink }}" class="" alt="{{ $scaled.Title }}">
		{{ end }}
	</a>
	{{ $bereich }}
	{{ $path }}
	<div class="pa2">
		<div class="w-100 mt1 f4-ns">
			{{ with .Site.GetPage "section" "$bereich" }}
			{{ .Title }}
		</div>
		<p class="f6 lh-copy measure">
			{{ .Description }}
		</p>
		<p><a class="link dark-red" href="{{ .RelPermalink }}">{{ .Site.Data.i18n.morelink}}</a></p>
		{{ end }}
	</div>
</article>
</div>

```

The debugging calls do this:

`{{ $bereich }}` expands to the right value of `.bereich`

But `{{ $path }}` expandes to `/$bereich/_index.md`

---

<div class="post-metadata">

**Author:** ![juh2](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/juh2/32/3980_2.png) [@juh2](https://discourse.gohugo.io/u/juh2)\
**Post date:** [September 11, 2019, 9:34am UTC](https://discourse.gohugo.io/t/passing-a-variable-to-a-partial-again/20729/7 "2019-09-11T09:34:22Z")

</div>

I get it working with this:

```
<div class="fl w-100 w-50-m w-20-l">
<article class="pa3 bg-washed-red b--white bw3">
	<a href="{{ with .site.GetPage "section" .bereich }}{{ .RelPermalink }}{{ end }}">
		{{ $path := delimit (slice "/" .bereich "/" "_index.md") "" }}
		{{ $preview := (site.GetPage .bereich).Resources.GetMatch "feature" }}
		{{ with $preview }}
		{{ $scaled := .Fill "566x378 Gaussian Center" }}
		<img src="{{ $scaled.RelPermalink }}" class="" alt="{{ $scaled.Title }}">
		{{ end }}
	</a>
	<div class="pa2">
		<div class="w-100 mt1 f4-ns">
			{{ with site.GetPage "section" .bereich }}
			{{ .Title }}
		</div>
		<p class="f6 lh-copy measure">
			{{ .Description }}
		</p>
		<p><a class="link dark-red" href="{{ .RelPermalink }}">{{ .Site.Data.i18n.morelink}}</a></p>
		{{ end }}
	</div>
</article>

```

No $-variable needed.
