Char Replace in Template Code?

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>,&nbsp; {{ 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?

There is both the replace and sanitizeurl template funcs, also the relURL or absURL may be of help in your case.

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>,&nbsp; {{ 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?


{{ lower(replace . " " "-") }}


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>,&nbsp; {{ end }}

Yes … That function is also used internally Hugo for all (or most) URLs. The lower casing can be turned off by setting DisablePathToLower=true.

Thanks for your help.