Inconsistent loop over page variable

When results are inconsistent from one build to the next, the first thing to consider is parallelism. From https://gohugo.io/troubleshooting/build-performance:

Hugo builds pages in parallel where multiple pages are generated simultaneously.

So, if two or more pages were built at the same time, what conflicts might occur to produce the observed behavior? In your example, it is related to the Scratch counters. You have a scope problem.

In the “Hugo .Scratch explained” article written by @regis a few years ago:

Remember that if you are inside a range on your index page, then your index page’s .Scratch will be $.Scratch while the page you are currently rangeing on, will be .Scratch .

You can resolve the problem with syntax changes, or by using a locally-scoped scratch.

However, with Hugo 0.48 and later, you do not need to use scratch variables to obtain the desired results.

{{ $ac := 0 }}
{{ with .Params.authors }}
{{ range sort . }}
    {{ $author := index $.Site.Data.authors . }}
    {{ with $.Site.GetPage (printf "/authors/%s" $author.name | urlize) }}
        {{ if eq $ac 0 }}
        <span class="no-wrap">
        <svg class="icon person"><use xlink:href="#person"></use></svg>
        {{ template "post-author" dict "name" $author.name "page" . }}
        </span>
        {{ else }}
        {{ template "post-author" dict "name" $author.name "page" . }}
        {{ end }}
    {{ end }}
    {{ add $ac 1) }}
{{ end }}
{{ end }}
4 Likes