I am trying to display a list of comma separated values, and don\'t want to display a comma after the last item (or the only item if there is only one).
My code so far:<
A nice trick you can use is:
Equipment:
{{$equipment := .Equipment}}
{{ range $index, $element := .Equipment}}
{{if $index}},{{end}}
{{$element.Name}}
{{end}}
This works because the first index is 0
, which returns false
in the if
statement. So this code returns false
for the first index, and then places a comma in front of each following iteration. This results in a comma separated list without a leading or trailing comma.