I have a <div>
block which I need to set to display:none
or display:block
based on the condition. The html looks like this,
<div style="display:none;">
//some html block content
</div>
I've tried the following code in thymeleaf,
<div th:style="${condition} == 'MATCH' ? display:block : display:none">
//some html block content
</div>
But the above expression is not working. throws org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression:
error message.
I can do th:classappend
to set some class and make this work but want to know if elvis/ternary operator will support on thymeleaf th:style
tag.
Solved it while posting the question,
th:style="${condition ? 'display:block' : 'display:none'}" >
would produce the necessary conditional style. If condition is true display is set to block and none if condition is false.
For admin,
th:style="${role == 'ADMIN' ? 'display:block' : 'display:none'}" >
the style is set to display:block
and for other roles the block is not displayed.
In simple case it could be written as
<div th:style="${filed==null} ? 'opacity:.3'">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With