Variable out of scope

Anybody tried to insert {{ hugo.IsProduction }} on _content.gotmpl for the load of the json file ?
Tried to use the code below, but it trows can't evaluate field isProduction in type interface {}

{{ if hugo.IsProduction }}
{{ $url := "data/bigfile.json" }}
{{ else }}
{{ $url := "data/smallfile.json" }}
{{ end }}

You have a scope problem. A variable initialized (:=) within an if, with, or range block is only available within the block. You need to initialize outside of the block, then assign (=) within the block.

{{ $url := "data/smallfile.json" }}
{{ if hugo.IsProduction }}
  {{ $url = "data/bigfile.json" }}
{{ end }}

https://gohugo.io/templates/introduction/#variables

The hugo.IsProduction template function was added in v0.66.0.

1 Like

Thank you very much!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.