I’m using the following code to parse the list of tags in the front-matter of my posts and display them as a list of clickable links at the end of each post:
{{ range .Params.tags }}<a class="tag" href="/tags/{{lower . }}">{{lower . }}</a>, {{ end }}
[The {{lower .}} lower cases each tag to avoid any case-sensitivity problems.]
I’ve run into a bit of a problem now, however. If the tag in the front-matter consists of two words [eg. “VW beetle” Hugo will generate the tags category as /public/tags/vw-beetle/, however my above code will generate the URL to that category page as mydomain/tags/vw%20beetle which obviously returns a 404.
Is there a way I can do a char replace within the template code, so I can substitute a - for any spaces which appear in multi-word tags?
Thanks. That’s solved my problem of substituting hypens for spaces in the URL:
{{ range .Params.tags }}<a class="tag" href="/tags/{{replace . " " "-" }}">{{ lower . }}</a>, {{ end }}
Now is there a way to combine the two template finctions, ie. lower and replace, so I’m doing the hyphen-for-space substitution AND making sure the result is lower-case?
Pseudo-code:
{{ lower(replace . " " "-") }}
EDIT:
Actually, I’ve just found the URLize function, which would seem to do what I want in one go. [I presume it also lower-cases any text in a URL?]
{{ range .Params.tags }}<a class="tag" href="/tags/{{ . | urlize }}">{{ lower . }}</a>, {{ end }}