Strange range behaviour: only some commands within range being executed

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?

errorf or warnf will evaluate a format string, then output the result to the ERROR or WARNING log (and only once per error message to avoid flooding the log). - errorf and warnf | Hugo

You could output more meaningful message with args, something like this.

{{ warnf "Second log: %s" $key }} 

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