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

前端 未结 4 779
面向向阳花
面向向阳花 2020-12-15 02:46

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:i         


        
相关标签:
4条回答
  • 2020-12-15 03:04

    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.

    0 讨论(0)
  • 2020-12-15 03:15

    This is what worked for me:

    th:if="${evaluation lt 49 and evaluation gt 29}"
    
    0 讨论(0)
  • 2020-12-15 03:21

    I did this to have multiple conditions in th:if in thymeleaf

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

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

    0 讨论(0)
  • 2020-12-15 03:23

    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 !

    0 讨论(0)
提交回复
热议问题