[原]百香菜
spring boot好像喜歡採用thymeleaf(百香葉)來做模板。
作為一個從.net過來的程式設計師,我覺得thymeleaf對母板頁的理解不到位,甚至是發生了偏差。
thymeleaf與asp.net的razor對母板頁的理解和處理,是截然不同的。
razor:
具體頁套用一個母板頁,將個性化內容寫到母板頁指定的相關區域。因此,具體頁只有個性化的內容。
thymeleaf:
沒有一個所謂的母板頁,而是各種支離破碎的公用片段。具體頁須自己去組織這些公用片段,這裡引用一點,那裡引用一點,縫百衲衣呢。好一個海納百川。
大家都說它很靈活,語法很強。問你死未。
話是這麼說,但我還要捏著鼻子記錄一下“心得”:
話說有一個片段集合檔案:_index.html,裡面定義了2個公用片段:header,js
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head th:fragment="header"> <meta charset="utf-8" /> <style type="text/css"> body{ margin:0; padding:0; background-color: #8EACDE; } </style> <script th:src="@{/js/jquery-1.11.1.min.js}" src="../js/jquery-1.11.1.min.js" type="text/javascript"></script> </head> <body> <div></div> </body> <script th:fragment="js"> alert("hello world!"); </script> </html>
好,具體頁要用它們啦:
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>統計結果</title> <th:block th:include="_index :: header" /> <!-- 引入片段header --> <style type="text/css"> #container{ height: 300px; } </style> <script src="http://192.168.0.211/pubzy/echarts3/echarts.min.js"></script> </head> <body> <div id="container"></div> </body> <script type="text/javascript"> <th:block th:include="_index :: js" /> <!-- 引入片段js --> </script>
thymeleaf3.0好像有所改善,但百衲衣的核心思想並沒有改變。