Using the categories of a post in the html class tag

I want to use assigned categories of a post in the html class tag:

<article class="post <catOneName> <catTwoName>">

If I use

<article class="post {{ .Params.categories }}">

I get

<article class="post [catOneName]">

Obviously, I don’t want the brackets surrounding it. I thought the trim function would help me with this but when I use

<article class="post {{ trim .Params.categories "[]" }}">

I get

<article class="post

The rest of the html gets truncated. The .html just stops at “post”.

What do I not understand and where in the documentation should I have looked to find the necessary information?

Respectfully,
…Smittie

Hello Smittie,

the categories from the frontmatter are a list of strings (your categories). If you just insert the list as it is in the class attribute

<article class="post {{ .Params.categories }}">

Go will use its standard formatting with the brackets. But Hugo provides a delimit template function that takes the strings from the list separates each with a delimiter. In your case a whitespace:

{{ delimit .Params.categories " " }}
1 Like

Does that mean the line in my template should look like this:

<article class="post {{ delimit .Params.categories " " }}">

Shazam!! That works!! Thank you.

…Smittie