.Site.Params.foo not available in a range loop but available outside?

Inside a partial, why does this work

          {{ $foo := .Site.Params.foo }}
          {{ range $action := .Site.Params.header.nav_links }}
            {{ if $foo }}
            <a href="{{ $action.url | relURL }}">{{ $action.label }}</a>
            {{ end }}
          {{ end}}

but this doesn’t

          {{ range $action := .Site.Params.header.nav_links }}
            {{ if .Site.Params.foo }}
            <a href="{{ $action.url | relURL }}">{{ $action.label }}</a>
            {{ end }}
          {{ end}}

?

config.yaml has

[...]
params:
  foo: true
  header:
    nav_links:
      - label: Home
        url: /
      - label: Support
        url: /support/
[..]

add $ to access the global variables inside range

         {{ range $action := .Site.Params.header.nav_links }}
+           {{ if $.Site.Params.foo }}
            <a href="{{ $action.url | relURL }}">{{ $action.label }}</a>
            {{ end }}
          {{ end}}
2 Likes

Thanks for clarifying that! As someone coming from programming languages, this Go templating seems like a wonderful messy usefulness :).

1 Like