Conditional setting custom variable


#1

Hi Guys,

I need a little help with setting a custom variable conditional. My current code isn’t working (think it’s related to go template scoping issues?).

Found https://gohugo.io/functions/scratch/ in the documentation. But I can’t get it to work right with structure of $custom or $default.

All help is welcome. Cheers

{{ if (or (.Params.hero.show) (.Params.defaults.hero) )  }}


{{ $custom := .Params.hero }}
{{ $default := .Site.Data.defaults.hero }}

{{ $custom }}
{{ $default }}

{{ if .Params.hero.show }}
  {{ $hero := $custom }}
{{ end }}

{{ if .Params.defaults.hero }}
  {{ $hero := $default }}
{{ end }}

<!--- template code -->

{{ end }}

Structure of $custom or $default


map[img:/img/hero.jpg size:medium text:Test ticker:true show:false]

#2

Little update. Got it working, but not in a ideal shape.

{{ if (or (.Params.hero.show) (.Params.defaults.hero) )  }}

{{ $custom := .Params.hero }}
{{ $default := .Site.Data.defaults.hero }}

{{ if .Params.defaults.hero }}
  {{ $.Scratch.Set "size" $default.size }}
  {{ $.Scratch.Set "text" $default.text }}
  {{ $.Scratch.Set "ticker" $default.ticker }}
  {{ $.Scratch.Set "img" $default.img }}
{{ end }}

{{ if .Params.hero.show }}
  {{ $.Scratch.Set "size" $custom.size }}
  {{ $.Scratch.Set "text" $custom.text }}
  {{ $.Scratch.Set "ticker" $custom.ticker }}
  {{ $.Scratch.Set "img" $custom.img }}
{{ end }}

{{ end }}