2

I'm attempting to create an element in Thymeleaf 3.0.3 (Spring 4.3.7) with a custom data-attribute:

<input th:attr="data-customAttr=${item.getMyAttr()}" />

If the result of item.getMyAttr() was 'someVal', then the rendered HTML comes out to be:

<input data-customAttr='someVal' />

However, if the result of item.getMyAttr() is an empty string, the custom attribute is thrown out by Thymeleaf altogether. The rendered HTML looks like:

<input />

I need to know whether the custom attribute was defined and empty or missing and undefined. According to this discussion: Are empty HTML5 data attributes valid? , empty data-attributes should be perfectly valid.

After a little digging I ran into the following test case that seems to show Thymeleaf can't render empty data-attributes, which is why it's throwing them out before render-time. The following Thymeleaf snippet renders just fine:

<input th:attr="__${'data-customAttr=' + 'someVal'}__" />

While both of these throw an error:

<!-- Throws: 'org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as assignation sequence' -->
<input th:attr="__${'data-customAttr=' + ''}__" />
<input th:attr="__${'data-customAttr'}__" />

Is this a bug in Thymeleaf? Any help would be greatly appreciated. I'm happy to provide any other relevant information as well.

Edit: This is a feature I'm making heavy use of in a fairly large application. While I'm aware that there are several workarounds, they all compound the amount of code I have to write/maintain just to accomplish a very simple task. I'm hoping for a long-term solution, even if that solution is "This is a bug, please report it".

Community
  • 1
  • 1
user7382368
  • 293
  • 1
  • 9

1 Answers1

3

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:

<input th:attr="__${'data-customAttr=' + ''}__" />
<!-- evaluates to -->
<input th:attr="data-customAttr=" />
<!-- which is an invalid expression -->

Same with

<input th:attr="__${'data-customAttr'}__" />
<!-- evaluates to -->
<input th: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.

Metroids
  • 18,999
  • 4
  • 41
  • 52
  • Thanks! This is one of many workarounds I'd considered. Unfortunately, I'm working on a fairly large application and can only tolerate so many solutions that are more complicated than the problem they solve. I've edited the question to be a little more specific about what I'm looking for. Do you know if this is a bug or intentional? I'm happy to report it if it's a bug. – user7382368 Mar 21 '17 at 19:36
  • I don't believe it's a bug... more than likely a design choice. – Metroids Mar 21 '17 at 20:27
  • Understood. Thanks again for the consultation! It looks like I can't (publicly) award you a point with my low reputation. – user7382368 Mar 21 '17 at 20:36