2.7 條件表達式
模板名稱:condition-express.html
<1>a ? b:c (if then:else)
<2>a?c (if else)
條件表達式(
condition
,
then
和
else
)的所有三個部分都是表達式,這意味着它們可以是變量(
${...}
,
*{...}
),消息(
#{...}
),URL(
@{...}
)或文字(
'...'
))。
e.g.
<li>? 'xx' :'xx'(if ? then:else)<span th:class="${title} ? 'green' :' red'">樣例一</span></li> <li>?'xx'(if ? then)<span th:class="${title1} ? 'green'">樣例二</span></li>
<!-- 嵌套 --> <tr th:class="${row.even}? (${row.first}? 'first' : 'even') : 'odd'"> ... </tr>
<!-- Else表達式也可以省略,在這種情況下,如果條件為false,則返回null值 --> <tr th:class="${row.even}? 'alt'"> ... </tr>
2.8 默認表達式(Elvis operator )
模板名稱:condition-express.html
語法: ?: (if:defaultValue)
the first one is used if it doesn’t evaluate to null, but if it does then the second one is used.(值不為null使用第一個,否則第二個)
e.g.
<div th:object="${session.user}"> ... <p>Age: <span th:text="*{age}?: '(no age specified)'">27</span>.</p> </div>
equals:
<p>Age: <span th:text="*{age != null}? *{age} : '(no age specified)'">27</span>.</p>