Unexpected result appending a slice to a slice of slices

Given the following (a simplified excerpt from much longer, not-yet-published code):

{{- $tw := slice (slice "50" "abc" "200") -}}
{{- $tw = $tw | append (slice "100" "def" "300") -}}
{{- $tw = $tw | append (slice "200" "ghi" "400") -}}
{{- $tw = $tw | append (slice "300" "jkl" "500") -}}
{{- $tw = $tw | append (slice "400" "mno" "600") -}}
{{- $tw = $tw | append (slice "500" "pqr" "700") -}}

{{- range $tw -}}
	{{- $twNum1 := index . 0 -}}
	{{- $twLtr := index . 1 -}}
	{{- $twNum2 := index . 2 -}}
	<p>{{ $twLtr }}</p>
{{- end -}}

…I would expect the output to be:

<p>abc</p>
<p>def</p>
<p>ghi</p>
<p>jkl</p>
<p>mno</p>
<p>pqr</p>

…but what should be the second line (from the first-appended slice) is turned into three lines, each of which has the ASCII value of the last character in the corresponding item of the slice "100" "def" "300":

<p>abc</p>
<p>48</p>
<p>101</p>
<p>48</p>
<p>ghi</p>
<p>jkl</p>
<p>mno</p>
<p>pqr</p>

I have also tried outputting $twNum1 and $twNum2 but, in each case, the second line becomes three separate lines with similar character-to-ASCII-value stuff, rather than the expected one-line output of the desired variable.

Have had no luck finding parallels either here in the Discourse or in general web searches. Will very much appreciate whatever help anyone can provide.

Not an answer, but this is a simplified example:

{{ $obj := slice (slice "a") }}
{{ $obj = $obj | append (slice "b") }}
{{ $obj = $obj | append (slice "c") }}

<pre>
{{ jsonify (dict "indent" "  ") $obj }}
</pre>

result

[
  [
    "a"
  ],
  "b",    <-- ???
  [
    "c"
  ]
]

This isn’t new behavior (for example, reproduced with v0.76.0).

The only open issue related to the append template function is:
https://github.com/gohugoio/hugo/issues/10458

1 Like

Interesting. When I try this with my previous example:

<pre>
	{{ jsonify $tw}}
</pre>

…I get:

<pre>
	[["50","abc","200"],"100","def","300",["200","ghi","400"],["300","jkl","500"],["400","mno","600"],["500","pqr","700"]]
</pre>

Note the missing inner brackets around the second (first-appended) slice, which is the problem child in this case. So your suggestion does at least show another way in which something weird is going on with that particular slice. What it means, I have no idea.

Guess I’ll find another way to accomplish what I was trying to do. :slight_smile: Thanks as always, @jmooring!

https://github.com/gohugoio/hugo/issues/11004

1 Like

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