CSS Classes
Last updated
Was this helpful?
Last updated
Was this helpful?
Similar to HTML , Melody supports the usual HTML method of adding classes to a HTML element via the class
attribute:
Melody also supports the ternary if
operator Twig method of adding classes that are dependent on some other variable:
This works ok when you have only one or two conditional classes, but if you have a number of them, it can begin to look a bit cramped and ugly.
To help combat this, Melody also has the classes
filter which is similar to, and works well with, the attrs
filter:
With the classes
filter, the keyword base
is used to specify the class list that will always be added to the element. Any other classes are then specified afterwards, with their class name (btn--disabled
) specified on the left and their condition (isDisabled
) specified on the right.
The above example will generate one of the following DOM structures:
If isDisabled
is true
:
If isDisabled
is false
:
base
does not have to be used if no default classes are to be added:
The classes
filter can also be used with default classes but without using base
:
As class
is an attribute itself, it can also be added via the attrs
filter, and classes
also works with it:
You might only do this if you are already using the attrs
filter and decide to filter all an element's attributes through it.
If you are using the classes
filter and want to add classes whose names are wholly or partially contained within a variable, then you need to do the following: