Note that you can combine the .classname
syntax with the class
attribute:
// Input:
.foo.bar(class="baz qux")
// Output:
<div class="foo bar baz qux"></div>
And the class
attribute also supports arrays and objects for more advanced use cases.
From the Class Attributes section in the documentation (slightly modified for clarity):
The class
attribute can be a string,
like any normal attribute;
but it can also be an array of class names,
which is handy when generated from JavaScript.
Input:
- const classes = ['foo', 'bar', 'baz']
a(class=classes)
//- the class attribute may also be repeated to merge arrays
a.bang(class=classes class=['bing'])
Output:
<a class="foo bar baz"></a>
<a class="bang foo bar baz bing"></a>
It can also be an object which maps class names to true
or false
values.
This is useful for applying conditional classes.
Input:
- const currentUrl = '/about'
a(class={active: currentUrl === '/'} href='/') Home
a(class={active: currentUrl === '/about'} href='/about') About
Output:
<a href="/">Home</a>
<a class="active" href="/about">About</a>