有關freemarker的語法,迴圈,邏輯
==============常用語法=================
三.
EG.一個物件BOOK
1.輸出 ${book.name}
空值判斷:${book.name?if_exists },
${book.name?default(‘xxx’)}//預設值xxx
${ book.name!"xxx"}//預設值xxx
日期格式:${book.date?string('yyyy-MM-dd')}
數字格式:${book?string.number}--20
${book?string.currency}--<#-- $20.00 -->
${book?string.percent}—<#-- 20% -->
插入布林值:
<#assign foo=ture />
${foo?string("yes","no")} <#-- yes -->
=============邏輯判斷==================
2.
a:
<#if condition>...
<#elseif condit
inc 釋出於2007-09-08 16:18:57
ion2>...
<#elseif condition3>......
<#else>...
其中空值判斷可以寫成<#if book.name?? >
</#if>
b:
<#switch value>
<#case refValue1>
...
<#break>
<#case refValue2>
...
<#break>
...
<#case refValueN>
...
<#break>
<#default>
...
</#switch>
===============迴圈讀取=================
3.
<#list sequence as item>
...
</#list>
空值判斷<#if bookList?size = 0></#list>
e.g.
<#list employees as e>
${e_index}. ${e.name}
</#list>
<#if mole?default(0)!=0>
=============用來壓縮空白空間和空白的行===============
用例
<#assign x = " moo ">
(<#compress>
1 2 3 4 5
${moo}
test only
I said, test only
)
輸出
(1 2 3 4 5
moo
test only
I said, test only)
==============Sequence內建的計數器==============
3. Sequence內建的計數器: xxx_index
用途:顯示序號
模板:
<#list employees as e>
${e_index}. ${e.name}
</#list>
輸出:
1. Readonly
2. Robbin
==============Sequence內建的分段器=====================
4. Sequence內建的分段器: chunk
用途:某些比較BT的排版需求
模板:
<#assign seq = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']>
<#list seq?chunk(4) as row>
<ul>
<li><#list row as cell>${cell} </#list></li>
</ul>
</#list>
<#list seq?chunk(4, '-') as row>
<tr>
<td><#list row as cell>${cell} </#list></td>
</tr>
</#list>
輸出:
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
</ul>
<ul>
<li>e</li>
<li>f</li>
<li>g</li>
<li>h</li>
</ul>
<ul>
<li>i</li>
<li>j</li>
</ul>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
</tr>
<tr>
<td>e</td>
<td>f</td>
<td>g</td>
<td>h</td>
</tr>
<tr>
<td>i</td>
<td>j</td>
<td>-</td>
<td>-</td>
</tr>
=====================freemarker的list================================
Scalar String:${scalarString}
Scalar Number:${scalarNumber}
Object is:${scalarObject}
List使用樣例-List元素為Scalar物件:
<#list scalarList as value0>
Scalar List值:${value0}
</#list>
List使用樣例-List元素為User物件:
<#list userList as listUser>
List物件User Id值:${listUser.userId}
</#list>
Map使用樣例-Map Values元素為Scalar :
<#list scalarMap?keys as mykey>
Scalar Map key is :${mykey}
Scalar Map value is:${scalarMap[mykey]}
</#list>
Map使用樣例-Map Values元素為User物件:
<#list userMap?keys as key1>
<#assign mapUser="${userMap[key1]}" >
User Object is :${mapUser}
<#--
以下方法有問題
User is :${mapUser.userId} <br>
-->
</#list>
==============FreeMarker中list排序==============
升序:
<#list list?sort_by("time") as v>
</#list>
降序:
<#list list?sort_by("time") as v>
</#list>
===============freemarker在模板中定義變數==============
在模板中定義的變數有三種類型:
plain變數:可以在模板的任何地方訪問,包括include指令插入的模板,使用assign指令建立和替換
<#include "/WEB-INF/index/top.html">
==============freemarker.properties配置================
(1)解決輸出中文亂碼問題:
default_encoding=UTF-8
locale=zh_CN
number_format=#
(2)提高freemarker的效能
template_update_delay=60000
(3)freemarker的標籤種類:
${..}
# 代表是FTL tags
<#if ...></#if>
<#list totalList as elementObject>...</#list>
@ ,代表使用者自定義的標籤
<#-- --> 註釋標籤,注意不是<!-- -->
=====================將圖片整除換行====== <#if (u_index+1)%4=0>當圖片超過五個就換行=======
<table width="100%">
<tr>
<td><table width="100%">
<tr>
<#if map["最新-圖片"]?exists>
<#list map["最新-圖片"] as u>
<#if (u_index+1)%4=0>
<td><table width="70" height="65" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="center" valign="middle" bgcolor="#6F7074">
<a target="_blank" href="shownews.page?id=${u.id?default("")}&mole=2 "><img src="${u.chartpath?default('')}" width="67" height="41" border="0" /></a></td>
</tr>
<tr>
<td align="center" bgcolor="#6F7074">
<a target="_blank" href="shownews.page?id=${u.id?default("")}&mole=2 " class="tableff">
<@jf.greet title=u.title len=4 /></a>
</td>
</tr>
</table></td></tr><tr >
<#else>
<td><table width="70" height="65" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="center" valign="middle" bgcolor="#6F7074">
<a target="_blank" href="shownews.page?id=${u.id?default("")}&mole=2 "><img src="${u.chartpath?default('')}" width="67" height="41" border="0" /></a></td>
</tr>
<tr>
<td align="center" bgcolor="#6F7074">
<a target="_blank" href="shownews.page?id=${u.id?default("")}&mole=2 " class="tableff">
<@jf.greet title=u.title len=4 /></a>
</td>
</tr>
</table></td>
</#if>
</#list>
</#if>
</tr>
</table>
</td>
</tr>
</table>
</tr>
</table></td>
</#if>
</#list>
</#if>
</tr>
</table>
</td>
</tr>
</table>
===============================freemarker中在application
${Application.web_title}
在類裡:
list=dser.indexvideo(cvo);
if(list.size()!=0){
map.put("最新",list);}
在html靜態頁裡:
<#import "/WEB-INF/ftl/index.ftl" as jf>
(index.ftl
<#macro greet title len>
<#if title?length != 0>
<#if (title?length>len)>
${title[0..len-1]} <#else>
${title?trim}
</#if>
<#else>
${title?default("")}
</#if>
</#macro>
<#--時間比較-->
<#macro vstime start end>
<#if start?default("")==""||end?default("")=="">
--
<#else>
<#assign starts=start?replace("-","") >
<#assign ends=end?replace("-","") >
<#if (starts?number>ends?number)>
<font color="red">以過期</font>
<#else>
未過期
</#if>
</#if>
</#macro>
)
<#if map["最新"]?exists>
<#list map["最新"] as u>
${u.filepath?default('')}
<@jf.greet title=u.title len=6 />
</#list></#if>