Params not passing to partial [RESOLVED]

I have tried everything to get a simple param to pass to a partial, following allegedly working examples from this forum, but no luck.

my file has:

has_code = “true”

in my main index.html template I can see the param set:

{{ .Params.has_code }}

The word true is displayed within the body of my document.

at the top of my main index.html, I have:

{{ partial “head.html” . }}

and inside of head.html I have tried the following but never see “true” in the output:

{{ .Params.has_code }}
{{ $.Params.has_code }}
{{if .Params.has_code}}
{{ .Site.Params.has_code }}

nothing seems to work. I’m using Hugo v0.18.1

What am I missing here?

just FYI, I also tried putting {{ .Param “has_code” }} in my head.html, which did not display true, whereas putting the same code within the body of my index.html does display proper output.

I also saw another post that suggested {{ partial “head.html” $. }} instead of {{ partial "head.html . }} to get param to pass to partial but with 0.18.1 I get error: “unexpected <.> in operand”

Finally, I also tried removing the underscore in my param name… and also tried this with older 0.17 no luck. Running 64bit linux installed from .deb package.

Hello again,

Sleep does wonderful things to your brain.

So, this morning, I immediately realized what the actual problem is but not sure how to resolve it. I’m hoping this reply will “help you help me.”

Down in the body of my home page template (index.html), I have a range loop that allows my to only display the most recent non-draft “article” on the page:

{{ range first 1 (where (where .Site.Pages “Section” “articles”) “Draft” false) }}
… HTML to display the article’s title, content, etc. …
{{ end }}

Is there a way to pass the site page params from this range result back up to the head.html partial? I’m guessing the answer is no. Is there a workaround?

My intention was to have my head.html partial evaluate the “hasCode” and add a script tag to include, if true:

{{ $hascode := .Params.hascode }}
{{ if eq “true” $hascode }}
…script type=“text/javascript” src=’{{ .Site.BaseURL }}/js/hascode.js"…
{{ else }}
(do nothing)
{{ end }}

Any ideas?

Regards, and thanks for Hugo! I’m really enjoying the experience of wrapping my head around this awesome tool.

I think this is what you’re looking for:

@budparr Thanks for your reply.

I am guessing my only solution is to move my range loop to the top of the template, then do a map and pass everything down to the partial and the rest of the document. Before sleep, I tried doing a map and I guess I just thought it would magically pick up the page params from the loop, appearing later in the template, then pass them back up to where I included my partial.

Turns out that putting my range loop at the top of my document and simply putting {{ partial “head.html” . }} inside the loop resolved, solving one problem to create another. :slightly_smiling:

1 Like