模板 + 資料模型 = 輸出
${...} 標記動態資料位置,FreeMarker 合併輸出。<h1>Welcome ${user}!</h1>
<p>Our latest product: ${latestProduct.name}!</p>
資料模型 (樹狀結構):
(root)
|- user = "Big Joe"
|- latestProduct
|- url = "products/greenmouse.html"
|- name = "green mouse"
合併後輸出:
<h1>Welcome Big Joe!</h1>
<p>Our latest product: green mouse!</p>
latestProduct。user、name。animals[0].name。(root)
|- animals
|- mouse
|- size = "small"
|- price = 50
|- elephant
|- size = "large"
|- price = 5000
|- test = "It is a test"
| 元素 | 語法 | 說明 |
|---|---|---|
| 插值 | ${...} | 輸出變數值 |
| FTL 標籤 | <#directive> | 控制邏輯 (if/list/include 等) |
| 註解 | <#-- ... --> | 不輸出 |
<!-- 條件判斷 -->
<#if animals.python.price < animals.elephant.price>
Pythons are cheaper.
<#else>
Pythons are not cheaper.
</#if>
<!-- 迴圈輸出表格 -->
<table>
<tr><th>Name</th><th>Price</th></tr>
<#list animals as being>
<tr><td>${being.name}</td><td>${being.price} Euros</td></tr>
</#list>
</table>
<!-- 包含外部檔案 -->
<#include "/copyright_footer.html">
<!-- 組合使用:大動物加粗 -->
<#list animals as being>
<#if being.size = "large"><b>${being.name}</b><#else>${being.name}</#if>
${being.price} Euros
</#list>
若輸出中文亂碼,請在 Filter 中設定 response.setCharacterEncoding("UTF-8") 與 response.setContentType("text/html;charset=UTF-8")。