I am working on a page template that requires me to loop over a JSON file. For some reason, the instructions within the range
are only sometimes being executed, and I can see no reason for it.
I’ve trimmed the loop down to this:
{{ warnf "No of locations: %v" ( len $.Site.Data.locations ) }}
{{ range $key, $value := $.Site.Data.locations }}
{{ warnf "Current location: %v" $key }}
{{ warnf "Second log" }}
{{ end }}
And then, when I run Hugo, I get the following terminal output:
i.e., the loop is correctly iterating through all of the values, but the second instruction is only executing the first time.
The more complex loop I’m actually trying to do is
{{ .Scratch.Set "maxItems" 1 }}
{{ .Scratch.Set "numOfItems" 1 }}
{{ warnf "No of locations: %v" ( len $.Site.Data.locations ) }}
{{ range $key, $value := $.Site.Data.locations }}
{{ $childPosts := where $.Site.Pages ".Params.locations" "intersect" ( slice $key ) }}
{{ warnf "Current location: %v" $key }}
{{ warnf "Child posts: %v" $childPosts }}
{{ .Scratch.Set "numOfItems" ( len $childPosts ) }}
{{ if ( gt ( .Scratch.Get "numOfItems" ) ( .Scratch.Get "maxItems" ) ) }}
{{ warnf "Updating maxItems" }}
{{ .Scratch.Set "maxItems" ( .Scratch.Get "numOfItems" ) }}
{{ warnf "New maxItems: %d" .Scratch.Get "maxItems" }}
{{ end }}
{{ end }}
{{ warnf "maxItems: %v" ( .Scratch.Get "maxItems" ) }}
which produces the following terminal output:
Again, it isn’t running all of the commands on every iteration, but no issues are raised.
I’m using hugo v0.111.3-5d4eb5154e1fed125ca8e9b5a0315c4180dab192+extended linux/amd64
In that second console screenshot you can see some other warnings peppered throughout from other templates. Does this suggest that the problem is some sort of multi-thread/concurrency issue?