[solved] Unsure how to set variable value after declaring

here’s the code I’m trying to get to work:

{{ $maxCategories := 0 }}
{{ range $name, $taxonony := .Site.Taxonomies.categories }}
   {{ $pageCt := len $taxonomy.Pages }}
   {{ if gt $pageCt $maxCategories }}
      {{ $maxCategories := $pageCt }}
   {{ end }}
{{ end }}

this seems to “redeclare” a new maxCategories variable inside the range loop instead of setting the variable outside the loop. How do I make this work? Also, is there a better way to do “max” ?

1 Like

This is a known limitation (and a major gotcha) in Go templates.

To work around it, have a look at:

2 Likes

Thank you. I spent an hour pouring over GoLang documentation, and trying to find hints of this in Hugo documentation, finally I gave up. This definitely needs to be in the documentation.

Documentation could always be improved, but this has been a hot topic on this forum, so I would be really surprised if it doesn’t show up in obvious searches.