I have a thymeleaf fragment to create input fields like:
<div th:fragment="formField">
<input th:type="${type}" th:errorclass="field_error" th:field="*{__${field}__}" th:placeholder="#{__${placeholder}__}" />
</div>
This fragment is e.g. used like:
<div th:replace="fragments :: formField (type='password', field='password', placeholder='resetPassword.form.password')">
Now the autofocus attribute should be added or not to the input field based on the parameters of the fragment. Using the fragment e.g. like this should add the autofocus attribute:
<div th:replace="fragments :: formField (type='password', field='password', placeholder='resetPassword.form.password', autofocus='autofocus')">
I could not find a way to add the autofocus attribute conditionally to the input tag based on the fragment parameters. I tried using th:attr but always ended up in syntax errors.
Is there a way to create html attributes conditionally with thymeleaf?
th:withattribute from thymeleaf