[SOLVED] Accessing Site Params

I am generating a series of “partials” using this code to pull from an array in my config file.

config.json
"params": { "componentsdir": "widgets/", "components": ["about_me.html", "archive.html"} }

sidebar.html
{{ range $index, $component := $.Site.Params.components }} {{ $widget := (partial (printf "%s%s" $.Site.Params.componentdir $component )) }} {{ $widget }} {{ end }}

The partials are generating just fine. The issue is that within those partials I am calling a series of parameters. Using them on their own, they work as they should. But when used through that sidebar function they don’t display the parameters as it should.

i.e
about_me.html
<p class="side-content">{{ .Site.Params.sitePurpose}}</p>

So bottom line, Im not sure how to use the right syntax to be able to call the Params within the partials.

What Hugo version do you use?

v0.17

OK I see it. You need to pass the current page as the last argument to partial, i.e.:

  {{ $widget := (partial (printf "%s%s" $.Site.Params.componentdir $component  ) $. ) }}

It gave me an “Unexpected Dot” error
ERROR: 2016/11/21 16:24:29 template.go:477: template: theme/partials/sidebar.html:4: unexpected <.> in operand

Is it because my sidebar.html is a partial?

No … you can try to pull the “.” into a variable and pass that on, but I’m guessing a little too much here.

This is what I have, and its still giving me an unexpected dot error. :confused:

{{ $. := . }} {{ range $index, $component := $.Site.Params.components }} {{ $widget := (partial (printf "%s%s" $.Site.Params.componentdir $component ) $. ) }} {{ $widget }} {{ end }}

{{ $. := . }}

That was not what I meant – I assume $. is reserved.

Try

{{ $page := . }}

etc.

1 Like