Scratch.Add creates Arrays sometimes

Hi,
I tried to order images in buckets to display a list of images in columns. 4 month ago everything worked fine. But suddenly I have the problem, that after row 2 the input in in my first buckets is interpreted as an array while the other inputs are not.
I have tried to build the pages with hugo 5.1 and hugo 5.3, same problem.
And the problem is independent from my images (I deleted, copied and changed them to find out)

Here is my code:

{{ $imglist := (.Params.customer_galery) }}
{{ $len := (len $imglist) }}
{{ $rows := (div $len 4) }}
{{ $rest := (mod $len 4) }}

{{ if (gt $rows 0) }}
{{ range (seq 0 (sub $rows 1)) }}
{{ $row := . }}
{{ range (seq 0 3) }}
{{ $rowBucket := printf "%s_%d" "$rowbucket" . }}
{{ $imglistIndex := (add (mul $row 4) .) }}
{{ $.Scratch.Add $rowBucket (slice $imglistIndex) }}
{{ end }}
{{ end }}
{{ end }}

{{ $row := $rows }}
{{ range (seq 0 (sub $rest 1)) }}
{{ $rowBucket := printf "%s_%d" "$rowbucket" . }}
{{ $imglistIndex := (add (mul $row 4) .) }}
{{ $.Scratch.Add $rowBucket (slice $imglistIndex) }}
{{ end }}

{{ range seq 0 3 }}
  {{ $bucket := $.Scratch.Get (printf "%s_%d" "$rowbucket" .) }}
  {{ $bucket }}
{{ end }}

Result:
with 5 images: [0 4] [1] [2] [3] -> ok
with 9 images: [0 4 [8]] [1 5] [2 6] [3 7] -> images number 9 is inserted as an array
with 10 images: [0 4 [8]] [1 5 9] [2 6] [3 7] -> images number 10 is inserted normally again into second bucket
with 13 images: [0 4 [8] [12]] [1 5 9] [2 6 10] [3 7 11] -> images number 13 is inserted as an array to the first array also (like the first complicated image)

I cannot the see, what I have done wrong…

And my problem is, that if I try to range through the first bucket I get the following error, because the expected number is inside an array:

error calling index: cannot index slice/array with type []int64

A slice is an array, so perhaps here is part of the issue?

{{ $.Scratch.Add $rowBucket (slice $imglistIndex) }}

Thank you very much for your tip.

I slice the imglistIndex’s so that the buckets result in arrays.
When I remove the slicing of imglistIndex I get an error when I try to range over the buckets, that they are no longer arrays.

And I think this cannot be the problem, because I treat every imglistIndex the same, and sometimes it results in an array and sometimes not.

Maybe it’s a hugo bug?

Hi,

I don’t really have an explanation, but I do have some observations.

If you add these debugging lines after occurrences of {{ $.Scratch.Add $rowBucket (slice $imglistIndex) }}

Adding: {{ $imglistIndex }} 
with type: {{ printf "%T" $imglistIndex }} 
to {{ $rowBucket }} :
results in : {{ $.Scratch.Get $rowBucket }} {{ printf "%T"   ($.Scratch.Get $rowBucket) }} 
<br>

You get an output similar to this:

Adding: 0 with type: int to $rowbucket_0 : results in : [0] []int 
Adding: 1 with type: int64 to $rowbucket_1 : results in : [1] []int64 
Adding: 2 with type: int64 to $rowbucket_2 : results in : [2] []int64 
Adding: 3 with type: int64 to $rowbucket_3 : results in : [3] []int64 
Adding: 4 with type: int64 to $rowbucket_0 : results in : [0 4] []interface {} 
Adding: 5 with type: int64 to $rowbucket_1 : results in : [1 5] []int64 
Adding: 6 with type: int64 to $rowbucket_2 : results in : [2 6] []int64 
Adding: 7 with type: int64 to $rowbucket_3 : results in : [3 7] []int64 
Adding: 8 with type: int64 to $rowbucket_0 : results in : [0 4 [8]] []interface {} 
Adding: 9 with type: int64 to $rowbucket_1 : results in : [1 5 9] []int64 
Adding: 10 with type: int64 to $rowbucket_2 : results in : [2 6 10] []int64 
Adding: 11 with type: int64 to $rowbucket_3 : results in : [3 7 11] []int64 
Adding: 12 with type: int64 to $rowbucket_0 : results in : [0 4 [8] [12]] []interface {} 

Again, I don’t have an explanation, but perhaps, modifying your code a little:

{{ if (gt $rows 0) }}
  {{ range (seq 0 (sub $rows 1)) }}
  {{ $row := . }}
    {{ range (seq 1 4) }}
      {{ $rowBucket := printf "%s_%d" "$rowbucket" . }}
      {{ $imglistIndex := (add (mul $row 4) .) }}
      {{ $.Scratch.Add $rowBucket (slice (sub $imglistIndex 1)) }}
    {{ end }}
  {{ end }}
{{ end }}

{{ $row := $rows }}
{{ range (seq 1 $rest ) }}
  {{ $rowBucket := printf "%s_%d" "$rowbucket" . }}
  {{ $imglistIndex := (add (mul $row 4) .) }}
  {{ $.Scratch.Add $rowBucket (slice (sub $imglistIndex 1)) }}
{{ end }}
RESULT:
{{ range seq 1 4 }}
  {{ $bucket := $.Scratch.Get (printf "%s_%d" "$rowbucket" .) }}
  {{ $bucket }}
{{ end }}

should give you this output:

RESULT: [0 4 8 12] [1 5 9] [2 6 10] [3 7 11]

Thank you so much. this solved my problem!