Hi, I’m trying to use Params from the frontmatter in the template to load js only under certain conditions, but it won’t work (using v0.121.1):
The Pages Frontmatter looks like this:
---
params:
use_babylon: true
use_lightbox: true
use_dflip: false
---
In my custom-script-footer.html
Partial-Template i have tried the following so far, following these guides:
{{ if .Params "use_babylon" }}
USE_BABYLON - script code goes here
{{ end }}
That doesn’t find anything and is always false
, so i checked that .
{{ . }}
That prints out: Page(/portfolio/plastik/portraitplastik-2/index.md)
so im shure I get the page Object at this point.
{{ .Params }}
that prints out
map[date:2023-08-06 12:51:00 +0000 +0000 description: draft:false images:[] iscjklanguage:false lastmod:2024-11-03 00:03:14 +0100 CET lead: menu:map[portfolio:map[identifier:portraitplastik-2-d52ef981-7487-4430-9a39-e82ab55a01bd parent:]] params:map[use_babylon:true use_dflip:false use_lightbox:true] publishdate:2023-08-06 12:51:00 +0000 +0000 title:Portraitplastik 2 weight:620]
So there seem to be a param
map with my parameters:
{{ .Params.params }}
gives me:
map[use_babylon:true use_dflip:false use_lightbox:true]
so, I tried that:
{{ in .Params.params "use_babylon" }} --> is always false
and
{{ if .Params.params "use_babylon" }}
USE_BABYLON - script code goes here
{{ end }}
leads to an exception.
Neither leads to the goal.
What am I missing here?
Best regards - carsten
1 Like
irkode
November 3, 2024, 4:05pm
2
See Params | Hugo
Either use dot notation or the index function.
Looks like you read the Page.Param docs - without s which has a different behaviour.
Props for the good description.
Thanks for the reply
Attempt chaining identifiers
{{ .Params.use_babylon -}} --> gives nothing
{{ .Params.params.use_babylon -}} --> exception
Exception: execute of template failed at <.Params.params.use_babylon>: can’t evaluate field use_babylon in type string
Attempt with index
because {{ .Params.params }}
gives me
map[use_babylon:true use_dflip:false use_lightbox:true]
I tried also the index like so
{{ index .Params.params "use_babylon" }}
Error: <index .Params.params “use_babylon”>: error calling index: index of type string with args [use_babylon] failed: cannot index slice/array with type string
{{ index .Params.params use_babylon }}
Error: function “use_babylon” not defined
So, that seemed not to work either.
Are you passing any context when you call the footer partial?
https://gohugo.io/troubleshooting/faq/#why-is-my-partial-template-not-rendered-as-expected
Are you call the partial
function or the partialCached
function?
in the Theme the footer/script-footer-custom.html
is called from script-footer.html
like so:
{{ partial "footer/script-footer-custom.html" . -}}
I have not yet found out where script-footer.html
itself is called.
I can’t figure out what you’re doing wrong based on the above. I suggest you share your repository, or create a test repository that demonstrates the problem you’re having.
okay, thank you. i prepare that in the next days.
irkode
November 3, 2024, 8:44pm
8
Update… ok reread the answers above…looke like you already tried…sry
So … rest of the post is obsolete…
Sry…deleted…
I go with @jmooring and we would need the complete context.
cnichte
November 21, 2024, 11:06pm
9
Hi folks i build dan example, but at last I updated my Hugo from 0.121.2
to 0.139.0
.
Result: The error is gone. Everything works as it should
system
Closed
November 23, 2024, 11:07pm
10
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.