Custom Variable does not change after iteration

Hugo Version:

Hugo Static Site Generator v0.80.0/extended linux/amd64 BuildDate: unknown

Please look at this code:

{{ $loaded := 0 }}
{{ $isloaded :=0 }}
{{ range .Data.Pages }}
    { if le $loaded 10 }}
        <a href="{{ .RelPermalink }}">{{ .Title }}</a><br>
        {{ $loaded }}
        {{ $loaded := ( add $loaded  1 ) }}
        {{ $loaded }}
    {{ else if eq $isloaded 0 }}
         ...<br>
         {{ $isloaded := 1 }}
    {{ end }}
{{ end }}

I expect the output to be:

blah
0 1 blah
1 2 blah
....

But the output is

blah
0 1 blah
0 1 blah
....

It seems that Custom Variable does not change after iteration. It confused me. Why does that happen?( Is that some issue related to scopes? ) And any solutions?

Thanks for reading this post.

You are not overwriting the variable, rather you are re-declaring it. Also in your code above you have missed a curly bracket i.e. { if le $loaded 10 }}

See this post about overwriting variables:

Hint: you declare a variable with := then you overwrite it with =

Thanks :slight_smile:

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