Inconsistent loop over page variable

I’m currently working on a theme and try to loop over two list variables in a page front matter:


- author-1
- author-2

- tag-2
- tag-1


To loop over these lists I’m currently using this loop structure:

It is working in general but the result seems to be different on every build and I don’t really get it:

On page tags/opinion/ it looks like this (left) but on page tags/hardware/ the same item looks like this (right - and that’s how it should look like):

All these items are generated by the same loop from the list partial. I could see different behaviors on multiple builds… Sometimes every badge has an icon sometimes there is no icon at all. Maybe someone of you has an idea.

When results are inconsistent from one build to the next, the first thing to consider is parallelism. From

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" $ | urlize) }}
        {{ if eq $ac 0 }}
        <span class="no-wrap">
        <svg class="icon person"><use xlink:href="#person"></use></svg>
        {{ template "post-author" dict "name" $ "page" . }}
        {{ else }}
        {{ template "post-author" dict "name" $ "page" . }}
        {{ end }}
    {{ end }}
    {{ add $ac 1) }}
{{ end }}
{{ end }}

Thanks! Works now, and I will read a bit more about scopes :slight_smile:

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