Thymeleaf 在頁面中直接顯示內容
阿新 • • 發佈:2020-08-03
Thymeleaf 在頁面中直接顯示內容
一般情況下 Thymeleaf 模板要輸出變數需要在某個標籤中(如<div>、<span>
)寫th:text
等屬性來實現。但有時我們希望想不寫在標籤中,直接輸出變數的值,比如在<title>
標籤中直接顯示變數msg
的值,而不需要包含在<span>
等標籤中。
解決方案一:
使用th:block
<title><th:block th:text="${msg}" /> - 伺服器錯誤。</title>
參考:https://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#synthetic-thblock-tag
解決方案二(推薦):
使用inline
<title>[[${msg}]] - 伺服器錯誤。</title>
Hello, [[${user.name}]]! //[[]]寫法會html轉義
Hello, [(${user.name})]! //[()]寫法不會html轉義
參考:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#inlining
解決方案三:
使用th:remove
<span th:text="${msg}" th:remove="tag"></span>