局部變量,th:with能定義局部變量:
<div th:with="firstPer=${persons[0]}"> <p> The name of the first person is <span th:text="${firstPer.name}">Julius Caesar</span>. </p> </div>
當th:with被處理,firstPer變量創建一個局部變量和變量添加到map自上下文,以便它是用於評估和其他上下文中聲明的變量從開始,但只有包含< div >標記的范圍內。
div th:with="firstPer=${persons[0]},secondPer=${persons[1]}"> <p> The name of the first person is <span th:text="${firstPer.name}">Julius Caesar</span>. </p> <p> But the name of the second person is <span th:text="${secondPer.name}">Marcus Antonius</span>. </p> </div>
th:width屬性允許重用變量定義在相同的屬性:
<div th:with="company=${user.company + ' Co.'},account=${accounts[company]}">...</div>