Ronalds Vilciņš

Comma-separated tags in Hugo

Rendering a list of tag links is easy until you need commas between them but not after the last one. This Hugo template uses the item index to add the delimiter only when another tag follows:

<strong>{{ Tags: }}</strong>
{{ range $key, $value := .Params.tags }}
   <a href="/tags/{{ $value | urlize }}">{{ $value }}</a>{{ if ne $key (sub (len $.Params.tags) 1) }}, {{ else }}{{ end }}
{{ end }}

The final condition compares the current index with the last index in .Params.tags. Change , to any other separator you prefer.

Enjoyed reading this?

If you found this article helpful, consider supporting my writing. Every little bit helps keep this blog going.

← Back to all articles