Create a list of taxonomy terms

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

{{ range $name, $taxonomy := .Site.Taxonomies.services }}
{{ $name }},
{{ end  }}

The above produces…

"				app,
				
				card,
				
				icon,
				
				logo,
				
				web, 

"

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 := .Site.Taxonomies.services }}
{{- $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 := .Site.Taxonomies.services }}
       <li><span class="filter" data-filter=".{{ $name }}">{{ humanize $name }}</span></li>
       {{ end }}
</ul>

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

</script>

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.