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.
Related articles
- Customizing Hugo’s RSS Feed
Adjust Hugo RSS titles, descriptions, content, and section filtering while keeping the generated feed valid and easy to discover.
- Organizing Hugo Content With Tags and Categories
Organize Hugo content with a small, deliberate set of tags, categories, or custom taxonomies that lead to useful archive pages.
- A More Useful 404 Page in Hugo
Turn Hugo's 404 page into a useful route back to the homepage, search, or content visitors may have wanted.