Irikos
Irikos

Reputation: 335

Local conditional variable thymeleaf

I'm trying to rewrite without duplication something like this:

<span th:if=${userRole == 'ADMIN'} style="display:inline;"> // display property for logic only 
    <div> ... </div>
</span>
<span th:unless=${userRole == 'ADMIN'} style="display:none;"> 
    <div> ... </div>
</span>

In the 2nd case we have the same code, just hidden. This way I have duplicated code. It would be better to have a variable change it's value to either "none" or visible" and use that on a single tag.

How can I do it without duplication, implementing the following logic:

$variable = "display: none;"
th:if=${userRole == 'ADMIN'} $variable="display: visible"

<span style="display: $variable;"> 
    <div> ... </div>
</span>

Upvotes: 0

Views: 1941

Answers (1)

Ayush
Ayush

Reputation: 759

You can use a ternary operator (? :) to do it in one line without duplication.

th:style="${userRole} == 'ADMIN' ? 'display:inline' : 'display:none'"

I hope this is what you are looking for.

Upvotes: 2

Related Questions