Passing a variable to a partial, again

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.

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

{{ $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.

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

Thanks a lot for your hints.

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

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 :wink: )

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

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.