Creating Empty Data-attributes In Thymeleaf
I'm attempting to create an element in Thymeleaf 3.0.3 (Spring 4.3.7) with a custom data-attribute: If the result of
Solution 1:
The __
syntax isn't anything special... it just makes it run the evaluator twice. The reason it doesn't work is because in each case, it resolves to an invalid thymeleaf expression.
For example:
<inputth:attr="__${'data-customAttr=' + ''}__" /><!-- evaluates to --><inputth:attr="data-customAttr=" /><!-- which is an invalid expression -->
Same with
<inputth:attr="__${'data-customAttr'}__" /><!-- evaluates to --><inputth:attr="data-customAttr" /><!-- which is again, an invalid expression -->
It does appear that thymeleaf won't add empty attributes. So I think the best you are going to do is something like this:
<input th:attr="data-customAttr-exists=${item.getMyAttr() != null}, data-customAttr=${item.getMyAttr()}" />
And you'll have to check the first property to know whether or not it exists, and the second property for the value. This should allow you to know all cases.
Post a Comment for "Creating Empty Data-attributes In Thymeleaf"