Calling a Partial in a with/end breaks .Params usage

GitHub - anthonylehecka/intheHOAB: Hugo Version for the entire project

Lines in question from Layouts/Pages-Shared-Layouts/
Aside: All I was really trying to do was call a partial with a variable name but have given up on that for today, even with many sites saying it can be done.
Ok, let’s go to Plan B. No case statement in Hugo but {{ with }} was suggested.

Start with what works. A-OK, no problems
{{ partial “partial-Poet-Dante.html” . }}

Then go to the {{ with }} version, one for each of 24+ posts. Yes this is stupid, but if someone can show me a working {{ partial $usethispartial . }}, I’ll use it. Been through printf, Nils and nulls and partial is expecting a string etc. Keeps insisting it’s not a string.

Back to the problem code:
{{ $callthispartial := .Params.bodyContentPartial }}
{{ with (eq $callthispartial “partial-Poet-Dante.html”) }}
{{ partial “partial-Poet-Dante.html” . }}
{{ end }}

Gives me
render: failed to render pages: render of “page” failed: “C:\Hugo\Sites\InTheHeartOfAllBeings\layouts\Pages-Shared-Layouts\Post-Godly-Quality.html:57:11”: execute of template failed at <partial “partial-Poet-Dante.html” .>: error calling partial: “C:\Hugo\Sites\InTheHeartOfAllBeings\layouts\partials\partial-Poet-Dante.html:14:24”: execute of template failed at <.Params.Post_Sanskrit_Text>: can’t evaluate field Params in type bool

C:\Hugo\Sites\InTheHeartOfAllBeings\layouts\Pages-Shared-Layouts\Post-Godly-Quality.html:57:11:

The only difference is I am now calling the partial inside of the {{ with }}. The key message seems to be “can’t evaluate field Params in type bool”

Oddly enough, this works when {{ with }} is changed to if
{{ if eq $callthispartial “partial-Poet-Dante.html” }}
{{ partial “partial-Poet-Dante.html” . }}
{{ end }}
That works fine. It seems to be limited to the {{ with }} block.

https://gohugo.io/functions/go-template/with/

Binds context (the dot) to the expression and executes the block if expression is truthy.

The “expression” in your case is (eq $callthispartial "partial-Poet-Dante.html"), which returns true or false.

So within the block the context (the dot) is true.

So you’re calling a partial, passing the dot, which is true. That’s why the partial is complaining.

Yup. Exactly what the {{ with }} documentation says. I was going by an old support answer of using {{ with }} as a way of simulating the functionality of a case statement. Thank you for the quick response.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.