Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have multiple condition in an th:if tag using thymeleaf

I have a text to render in three different possible colors using thymeleaf.

So the code I've made so far to test the value is:

th:if="${evaluation} > 50"
th:if="${evaluation} < 30"

And that works well.

But the third test is for values between those two. So I tried:

th:if="(${evaluation} < 49) ∧ (${evaluation} > 29)"

but it's not working, I've got this error while parsing:

org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "(${evaluation} < 49) &and; (${evaluation} > 29)" (/property.html:41)

Of course, these lines are between tags since the first two are working properly.

Maybe the and operand is not correct, but the documentation of thymeleaf is not really explicit on those operands.

All ideas are welcome!

Update: I got the answer from the thymeleaf forum. The way to do it is:

th:if="${evaluation &lt; 49 and evaluation &gt; 29}"

Problem solved!

like image 387
brnrd Avatar asked Apr 15 '13 15:04

brnrd


People also ask

How do you put or condition in Thymeleaf?

Simple Conditionals: "if" and "unless" Thymeleaf provides th:if and th:unless conditional attributes to exclude elements based on a provided condition in the rendered document. Thymeleaf engine will evaluate the condition th:if="${user. active}" defined on the tr HTML element.

How do you check or condition in Thymeleaf?

To achieve similar to if-else condition in Thymeleaf we can use th:switch attribute. Thymeleaf at the first step will evaluate ${condition} expression and if the value is true it will print p tag with TRUE text. When an evaluated value is different than the true the Thymeleaf engine will generate p element with FALSE.

How do you use the operator in Thymeleaf?

Instead of using conditional operators && and || in the expression like we use in Java and Javascript, in Thymeleaf we use the text AND and OR for comparison.

How do you compare values in Thymeleaf?

You can compare values and expressions using the >, <, >= and <= Comparators. These operators behave the same way as they would behave in most of the programming languages.


3 Answers

I got the answer from the thymeleaf forum. The way to do it is :

th:if="${evaluation &lt; 49 and evaluation &gt; 29}"

Problem solved !

like image 197
brnrd Avatar answered Oct 16 '22 10:10

brnrd


This is what worked for me:

th:if="${evaluation lt 49 and evaluation gt 29}"
like image 28
user2779653 Avatar answered Oct 16 '22 09:10

user2779653


In my opinion, a better and more maintainable solution could be to write the evaluation code in a proper class.

class Evaluator{

private int value;
....

public boolean isBounded() {
    return value < 49 && value > 29;
}

then in thymeleaf, call the function:

<p th:if="${evaluator.isBounded()} ...

Some benefits:

  1. Cleaner template.
  2. Control in java code.
  3. Isolation. More complex evaluations could be written without changing the template.

I hope this helps.

like image 16
Francisco Perez Pellicena Avatar answered Oct 16 '22 11:10

Francisco Perez Pellicena