I came across a weird error that I could boil down to this minimal example:
{{ $c := delimit (slice "c") " " }}<!-- should be 'c' -->
{{ $abc_slice := slice "a" | append "b" $c }}<!-- should be ['a','b','c'] -->
{{ $abc := delimit $abc_slice "+" }}<!-- should be 'a+b+c' -->
{{ errorf "'%s' (%T) - '%s' (%T)" $c $c $abc $abc }}
Put that in a shortcode and strangely enough, the result is:
'c' (template.HTML) - 'a+b+b+c' (template.HTML)
As you can see, b is doubled.
The problem goes away if you add | print to $c, making it a string. I wonder why it's not a string in the first place.
Thanks in advance for any comments and fixing.
There are two causes of this problem.
The return of the delimit function is template.HTML and a bug in the collections.Append function.
template.HTML is actually the same as string, but due to the nature of golang you cannot append to [] string. This can be verified with a simple code.
package main
import (
"fmt"
)
type inherit string
func main() {
var s []string
var txt inherit
txt = "test"
s = append(s, txt) // ./prog.go:15:12: cannot use txt (type inherit) as type string in append
fmt.Printf("%v %s\n", s, txt)
}
For collections.Append, the problem arises in the code above. If the type cannot be added to an existing slice, add the whole element once again.
template.HTML isn't "the same as string"; if it was, it would have no purpose.
Most helpful comment
There are two causes of this problem.
The return of the
delimitfunction istemplate.HTMLand a bug in thecollections.Appendfunction.template.HTMLis actually the same as string, but due to the nature of golang you cannot append to [] string. This can be verified with a simple code.https://github.com/gohugoio/hugo/blob/339ee37143ca5a6bb22bbc1b0468d785f450cfb7/common/collections/append.go#L64-L71
For
collections.Append, the problem arises in the code above. If the type cannot be added to an existing slice, add the whole element once again.