So I wanted to set a header class (e.g. colors scheme) at either the global, section, or page level. This is what I came up with, if it helps someone else. If there’s a better way to do it, please chime in.
1) Get page or config-set Param, with a default.
{{ $headerClasses := $.Param "headerClasses" | default "classes" }}
That one line gets us most of the way, but it doesn’t allow us to get a value set at the section level, so we turn to the clunky part:
2) set the section, and if the page has a section, get the parameter from the page, but if not, look to see if the section has one set.
{{ $section := .Site.GetPage "section" .Section }}
{{ if $section}}
{{ $headerClasses := $section.Param "headerClasses" | default $headerClasses }}
{{ $headerClasses := .Params.headerClasses | default $headerClasses }}
{{ $.Scratch.Add "headerClasses" $headerClasses }}
{{ end }}
We can’t get a variable from inside of a conditional, so use Scratch
3) Call it again to sweep up, either getting the scratch value or the originally set value:
{{ $headerClasses := $.Scratch.Get "headerClasses" | default $headerClasses }}
Here’s a GIST with the entire thing with inline comments.
The following forum posts informed the above:
how-to-override-a-site-param-with-a-section-param
cant-define-variable-inside-ispage-block
Note: This posting is as of Hugo v0.20.7