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:
<input type="input" placeholder="Please enter your name">
Dynamic HTML attributes
Melody also allows you to specify attributes that are fully dynamic, in the usual html way:
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:
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:
You can do so, also via the attrs filter and html's merge filter:
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 inputfield 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