Create a list of taxonomy terms

I am trying to create a comma separated list of taxonomy terms.

{{ range $name, $taxonomy := }}
{{ $name }},
{{ end  }}

The above produces…

"				app,


What I need is the whitespace and trailing comma removed liked this…

"app, card, icon, logo, web"

You’re not the first one asking this and luckily there are already some solutions for your problem:

As to whitespace, a “trick” is also to write it like this:

{{ range $name, $taxonomy := }}
{{- $name }},
{{ end  }}

The “-” removes whitespace in the direction of the hyphen.

Using Go Templates seems to assume I’m using a taxonmy template. I’m trying to do it on a sectional page, specifically so I can pass the string to my data-filters tags.

<li><span class="filter active" data-filter=".app, .card, .icon, .logo, .web">All</span></li>

I figured out a solution using javascript.

Section Page Template

<ul id="filters" class="clearfix">
       <li><span id="show-all" class="filter active" data-filter="old">All</span></li>
       {{ range $name, $taxonomy := }}
       <li><span class="filter" data-filter=".{{ $name }}">{{ humanize $name }}</span></li>
       {{ end }}

<script type="text/javascript">
var filter = "{{ range $name, $taxonomy := }}.{{$name}}, {{ end}}";
filter = filter.slice(0, -2);
document.getElementById('show-all').setAttribute('data-filter', filter );


In summary, create a a string variable using the Go Template range. the slice function takes out the space and trailing comma. Finally I update the data-filter by targeting the data-filter attribute.