I am trying the above.
Let’s say the cuisines of my pages are:
Page1.md cuisines: ["Indian"]
Page2.md cuisines: ["Chinese"]
Page3.md not set
Page4.md cuisines: ["Greek"]
Page5.md cuisines: ["German"]
Expected value of $cuisines => [“Indian”, “Chinese”, “Greek”, “German”]
Actual value of $cuisines => [“Indian”, “Chinese”, [“Greek”], [“German”]]
Somehow the add of the default slice in case of Page 3 is triggering the following to become nested? I’m not sure why this is happening. Has anybody faced this?
This one is not unset it’s an empty array dunno if your frontmatter looks like cuisines: []
but nevertheless good news:
I was able to reproduce that one in v0.118 AND with your new code in v0.143.0
after appending an empty array using {{ $a | append slice }} looks like next append thinks it has to add the rest as arrays, too.
I’ll need to dig a little into, but it could be a bug
in your special case you could swap the arguments doing {{ $a = ($c | default slice) | append $a }} but that looks weird. I also would suggest to skip that default stuff completely.
I think what you really want is:
the union of all the elements in cuisine without duplicates:
{{ define "main" }}
{{ $cuisines := slice }}
{{/* need to use site. here cause I'm not in a bundle */}}
{{ range site.RegularPages }}
{{ with .Params.cuisines }}
{{ $cuisines = union $cuisines . }}
{{ end }}
{{ end }}
{{ printf "%v" $cuisines }}
{{ highlight ($cuisines | jsonify (dict "indent" " ")) "JSON"}}
{{ end }}
Assuming cuisines is a taxonomy, it seems like this would be a better way to get a slice of all the terms:
{{ $cuisines := slice }}
{{ range site.Taxonomies.cuisines }}
{{ $cuisines = $cuisines | append .Page.Title }}
{{ end }}
{{ $cuisines }} → [Chinese German Greek Indian]
If you don’t want the global set of terms, but instead only those in the current page collection, use the GetTerms method:
{{ $cuisines := slice }}
{{ range .RegularPages }}
{{ range .GetTerms "cuisines" }}
{{ $cuisines = $cuisines | append .Page.Title }}
{{ end }}
{{ end }}
{{ $cuisines | uniq | sort }} → [Chinese German Greek Indian]
In both examples above we get a page reference, so we can use the title of the term page instead of the term itself. That allows you, for example, to use a term like “chinese” but populate the slice with “Chinese Food”.
If cuisines is not a taxonomy:
{{ $cuisines := slice }}
{{ range .RegularPages }}
{{ with .Params.cuisines }}
{{ $cuisines = $cuisines | append . }}
{{ end }}
{{ end }}
{{ $cuisines | uniq | sort }} → [Chinese German Greek Indian]
{{ $cuisines = union $cuisines . }}
{{ $cuisines | sort }} → [Chinese German Greek Indian]
I estimate your’s will be faster for large collections anything else here?
should have checked before… sounds related
seems to be another flaw where adding an empty slice turns the collections type → and after that adding more slices will create sub slices instead of expanded values.
result might lead to confusion and produce strange results when appending arrays: