Thymeleaf HTML if else在同一行

时间:2017-06-29 08:24:34

标签: html css thymeleaf

我在Thymeleaf HTML页面中有这段代码,我想知道是否可以将if和else放在同一个语句中

<div th:if="${book.htmlIconColor != null}" >
<i class="fa fa-exchange fa-2x" th:style="${'color:' + book.htmlIconColor + '; text-align: center;'}" aria-hidden="true"></i>                     
</div>
<div th:if="${book.htmlIconColor == null}" >
<i class="fa fa-exchange fa-2x"  aria-hidden="true"></i>                     
</div>

1 个答案:

答案 0 :(得分:2)

尝试这样的事情:

condition ? first_expression : second_expression;

例如

<i class="fa fa-exchange fa-2x" aria-hidden="true" th:style="${book.htmlIconColor} ? 'color:' + ${book.htmlIconColor} + '; text-align: center;' : 'text-align: center;'"></i>