HUGO

Odd behavior with appending to multidimensional arrays

I’m currently using append to create multidimensional arrays, but I’m running into some unexpected behavior. Normally, if I were to “directly” append $arr2 to $arr1, the elements of $arr1 and $arr2 would be used to form a new array:

{{ $arr1 := slice "a" "b" }}
{{ $arr2 := slice "c" "d" }}
{{ $arr1 | append $arr2 }} => [a b c d]

If I instead want to add $arr2 as an element to $arr1, I would instead do something like:
{{ $arr1 | append (slice $arr2) }} => [a b [c d]]
which generally works.

However, in some situations involving nested arrays, directly appending $arr2 to $arr1 does not join their elements together, instead adding $arr2 as an element to $arr1:

{{ $arr1 := slice (slice "a" "b") }}
{{ $arr2 := slice "c" "d" }}
{{ $arr1 | append $arr2 }} => [[a b] c d] (expected)
{{ $arr1 | append $arr2 | append $arr2 }} => [[a b] c d [c d]] (???)

I’m not entirely sure what causes this behavior, besides that it might have to do with using multiple different types in the same array. I am new to Hugo and Go templating, so please let me know if I’m doing anything incorrectly.

Hugo’s append implementation:
https://github.com/gohugoio/hugo/blob/master/tpl/collections/append.go#L22-L37

I am pretty sure the answer to your question is here:
https://golang.org/ref/spec#Passing_arguments_to_..._parameters

My take on this: if the “from” slice is not assignable to the “to” slice, a new slice is appended. But I’ve read the spec several times and I’m still not sure I understand it.

{{ $s1 := slice (slice "a" "b") }}
{{ $s2 := slice "c" "d" }}
{{ $s1 | append $s2 | append $s2 }} => [[a b] c d [c d]]
{{ $s1 | append ($s2 | append $s2) }} => [[a b] c d c d]

Thanks for your response. I do think it has to do with assignability, though I’m not familiar enough with Golang to make full sense of the code. I can fortunately work around this quirk by using a different system for my partials, but maybe it’d be a good idea to separate append into append and concat? That way, append only adds a new element to a slice whereas concat only combines the elements of multiple slices, leading to more predictable behavior.