Hello folks,
I want to iterate over the $.Param
method, using config.yaml
to store defaults, and then overriding on the page level as needed.
The loop works, but only prints the page level values. If no page level value is given, the loop does not fall back to the default.
I am defining my default params in config.yaml
like so:
params:
locationtext: "123 Main Street"
locationlink: "http://example.com"
and then looping inside my template using:
{{ range $.Params }}
<p>{{ . }}</p>
{{ end }}
Any help would be much appreciated.
Thanks in advance!
Hello @bronzehedwick,
your page values will not be replaced if they are not present because Hugo doesn’t defaults to the param values in the config file. .Site.Params
and .Params
are two completely different namespaces and independent from each other.
If you would like to set defaults you have do it explicitly. I haven’t tested the code below. But it should range over the page’s frontmatter, check if foo
(example value) aka. .
is present. Otherwise default to $.Site.Params.foo
.
{{ range $.Params }}
{{ default $.Site.Params.( . ) . }}
{{ end }}
Hm, interesting. I guess I was confused, because {{ $.Param "locationtext"}}
gets the default from config.yaml
, or the page value if present. Is this a totally different function than $.Params
?
The above code results in the following error:
ERROR: 2016/07/14 16:52:29 template.go:472: template: /Users/delucac/Sites/project/themes/my-theme/layouts/_default/baseof.html:6: unexpected <.> in operand
1 Like
It was worth a try. I’ve seen in other examples in the forums that the braces allow you to access variables dynamically based on the name. It have never tried it myself.
Howe[quote=“bronzehedwick, post:3, topic:3680”]
I guess I was confused, because {{ $.Param “locationtext”}} gets the default from config.yaml, or the page value if present. Is this a totally different function than $.Params?
[/quote]
The names of $.Param
(the templace function) and $.Params
(the variable that contains the frontmatter) confused me as well.
It’s correct the template function $.Param "foo"
defaults to the .Site.Params
name space of your config file if foo
isn’t defined in the frontmatter of a page. The function needs the parameter’s name to associate a default value.
You’re seem to default to values dynamically based on the presence of a variable in the frontmatter.
Setting the value explictly ({{ $.Param "foo" }}
) works because Hugo know which variable should be replaced.
But if you want to do this dynamically by ranging over the frontmatter $.Param
can’t know which value should be replaced if you don’t know its name. And it can’t be know known if it isn’t set in the frontmatter:
{{ range $key, $value := $.Params }}
{{ $.Param $key }}<br>
{{ end }}
Ahh got it, thanks so much for the explanation! This all makes a lot more sense now.
Hopefully someone else has better approach.
Here’s a way to explicitly set a variable to check page frontmatter first, then site params.
In the page frontmatter I have:
+++
scope = "page"
+++
Then in the site config, I have the same key as a param:
+++
[params]
scope = "site"
+++
Then in the page templage I have:
{{ $scope := index .Params "scope" | default .Site.Params.scope }}
{{ $scope }}
That’ll give me an output of “page” unless I take scope out of the frontmatter.
Is that what you’re looking for?
3 Likes