Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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!

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 !

Using a Date object I found the following works th:if="${date.month ge 4 and date.month le 4}" – Benjamin May 19, 2014 at 18:01 Same answer as the accepted one, but cleaner syntax. If this is confimed to work, it should be the accepted answer. – Jeroen Kransen May 19, 2016 at 9:32

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:

  • Cleaner template.
  • Control in java code.
  • Isolation. More complex evaluations could be written without changing the template.
  • I hope this helps.

    th:if="${object.getStatus()} == 'active' and ${object.getActiveDate()}" th:text="${#dates.format(object.getActiveDate(), 'yyyy-MM-dd')}"

    I added the and operator between conditions. You can also add or if needed.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.