0

I have the following Thymeleaf code, I want "--" to be printed if taxHistory.landValue equals to zero, and the actual value to be printed if taxHistory.landValue is greater than 0

<td th:if"${taxHistory.landValue==0}" th:text="--"></td>
<td th:unless="${taxHistory.landValue>0}" th:text="|${taxHistory.landValue}|"></td>

However I'm getting the following error

org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "0}" 

What is the correct way of doing this in Thymeleaf?

Arya
  • 8,473
  • 27
  • 105
  • 175

1 Answers1

2

There are multiple ways to do this, but you can use a ternary operator and print your string if your desired condition does not match:

<td th:text="${taxHistory.landValue &gt; 0 ? taxHistory.landValue : '--' }">[taxHistory.landValue]</td>

You can use the &gt; notation to keep the HTML formed properly. This assumes that a negative value would also print your string (you didn't mention behavior in that unusual case).

riddle_me_this
  • 8,575
  • 10
  • 55
  • 80
  • 1
    works well, do you know how we can add a string to `>[taxHistory.landValue]<`? I want to add a dollar sign behind it. I added it manually but it's not showing up on the webpage. – Arya Sep 02 '19 at 23:55
  • Take a look at this post (and my updated answer): https://stackoverflow.com/questions/14160304/how-to-format-the-currency-in-html5-with-thymeleaf – riddle_me_this Sep 03 '19 at 01:16
  • Note that what you have in your comment is the default value that would show if you just opened the HTML page in a browser. It would not be shown if the page is served on a server/container. It's best practice to add this default value so that you can see what the page looks like without serving up the page. This works well for UI designers. – riddle_me_this Sep 03 '19 at 01:21
  • Would something like this work? https://pastebin.com/kJmUg1NV I'm trying to get the result of the one line if statement into a variable then print it inline. It's giving an error now though. – Arya Sep 03 '19 at 01:52
  • Are you simply attempting to add a currency symbol to your evaluated value? Or do you somehow want to add a currency symbol to the default value (what appears between the tags without running a server)? – riddle_me_this Sep 03 '19 at 02:10
  • 1
    in the former case, something like this (untested): `[total assessment]` – riddle_me_this Sep 03 '19 at 02:15
  • I'd always try to make it as simple as possible... it's a nightmare to maintain otherwise :) – riddle_me_this Sep 03 '19 at 02:19