Search code examples
spring-bootthymeleafconditional-operator

How to dynamically add a variable to ternary operator in thymeleaf


I am trying to pass a variable from my spring boot controller to a heading with a ternary operator and nothing seems to work.

<h1 th:text="${createEntry} ? 'Create a new Entry:' : 'Edit Entry no. ${id}'"/></h1>

returns

Edit Entry no. ${id}

What is the correct syntax?


Solution

  • <h1 th:text=${createEntry ? 'Create a new Entry:' : 'Edit Entry no. ' + id}" />
    

    or

    <h1 th:text="${createEntry} ? 'Create a new Entry:' : 'Edit Entry no. ' + ${id}"/>