Attributes
Since html templates are HTML, you are encouraged to use semantic HTML when building your templates. Melody supports the addition of attributes in your html templates in the standard manner:
Dynamic HTML attributes
Melody also allows you to specify attributes that are fully dynamic, in the usual html way:
Attributes with Dynamic Names
However, in order to specify attributes that have a dynamic name, you'll need to use the attrs
filter:
The attrs
filter converts the given attribute map to a structure that can be picked up by the Melody compiler.
Multiple dynamically named attributes can also be added:
Setting attribute values
To set an attribute to a specific value depending on some condition, you can do the following:
Adding existing attributes
If you already have an object that contains existing attributes that need to be additionally added to an element:
Conditional empty attributes
HTML has the concept of empty attributes whose pure presence has a meaning.
A prominent example of that is the checked
attribute of an input
field of type checkbox
.
Melody has full support for those attributes, however, if you want to conditionally include them, you'll need to use the attrs
filter again:
In this case, the checked
attribute will only be added if the isChecked
property is true
.
Attributes are NOT added to the rendered HTML when...
its value is
false
its value is
undefined
its value is
null
its value is
''
it was previously specified on the element in question
Last updated
Was this helpful?