Thymeleaf is a template engine for Java-based applications. The engine comes with all the necessary methods to manipulate DOM used in the presentation layer. Beside all commonly used HTML attributes, we can create our custom once.
2. Custom attributes using
You can create custom attributes using
In the following example we add two attributes:
data-customer-name to a link element:
This will be rendered to:
Note that the name of the attribute is discretionary, however to make our document compatible with HTML5 attributes should start with
In order to append a conditional attribute, you put the condition on the attribute's value, like in the following example:
If slug is null, the previous statement will evaluate to:
If slug is not null (value set to my-slug), the statement evaluates to:
In this article, we showcased how to create custom attributes in Thymeleaf. To extend the knowledge about attributes we also presented how to add attributes conditionally. Every web developer knows that attributes in HTML documents are extremely helpful and commonly used for storing important information about presented data.