Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf change variable

I am new in Thymeleaf and i need to do something like this:

<div th:each="element : ${list}" th:with="test=false">
    <div th:each="element2 : ${list2}">
        <div th:if="element2.name == 'someName'">
            <div th:with="test=true">test changed</div>
        </div>
    </div>
    <div th:text="${test}"></div>
</div>

If i try this code i see "test changed" but my variable test is always false

like image 956
Dawid K Avatar asked Mar 23 '14 21:03

Dawid K


1 Answers

A with expression creates or overrides a local variable.

That means that the modified variable is only accessible inside the element you declared the with expression on.

In your case the output of test is written outside of the modification div so you get the result from outside.

like image 147
Martin Frey Avatar answered Oct 19 '22 12:10

Martin Frey