Currently trying to make a nice gallery of all my photos. In doing so, I want to take a slice that contains all images, and turn it into a slice of slices, with each of those nested slices containing up to three images, representing one row of photos in my table.
(Using flexboxes is not an option, there are reasons behind my madness.)
Currently I have this code:
{{- define "_partials/subdivide-list.html"}}
{{ $input := .list}}
{{ $itemsPerRow := .itemsPerRow }}
{{ $output := slice}}
{{/* Maths in Hugo is too much of a pain for me right now, so we'll just do what is effectively an endless loop and bail
* when done. This will obviously break if you have more than 10000000 * $itemsPerRow images but I'd say, don't worry about it.
*/}}
{{ range 10000000}}
{{ if gt (len $input) $itemsPerRow }}
{{ $row := first $itemsPerRow $input }}
{{ $input = after $itemsPerRow $input }}
{{ $output = $output | append $row }}
{{ else if eq (len $input) 0 }}
{{/* avoid having a last "slice" that has zero items */}}
{{ break }}
{{ else }}
{{ $output = $output | append $input }}
{{ break }}
{{ end }}
{{ end }}
{{ return $output }}
{{- end}}
The problem is here:
{{ $output = $output | append $row }}
$row, in this context, is a slice, as is $output.
The problem here is that append is overloaded with the following overloads:
collections.Append ELEMENT [ELEMENT...] COLLECTION
collections.Append COLLECTION1 COLLECTION2
As both $output and $row are collections, the latter overload triggers, and unfortunately, the semantics of that overload merge the two slices into one slice, rather than adding the first slice into the second as a single item. So I end up with a slice of images (which is identical to the $input parameter!) rather than a slice of slices of images.
The documentation at collections.Append has a couple of examples for adding a slice to a slice as a slice, but I am honestly stumped to figure out exactly how these examples even work, let alone how to adapt them to my usecase.
Any help?