<#if condition>
? ...
<#elseif condition2>
? ...
<#elseif condition3>
? ...
...
<#else>
? ...
</#if>
二:switch
<#switch value>
? <#case refValue1>
??? ...
??? <#break>
? <#case refValue2>
??? ...
??? <#break>
? ...
? <#case refValueN>
??? ...
??? <#break>
? <#default>
??? ...
</#switch>
三:list,break
<#list sequence as item>
??? ...
</#list>
兩個特殊的循環(huán)變量:
item_index: 當(dāng)前變量的索引值。
item_has_next: 是否存在下一個對象。
可以用<#break/>指令離開loop循環(huán)。
四:include指令
<#include filename>
或者
<#include filenameoptions>
filename: 表達(dá)式轉(zhuǎn)換為字符串
options: encoding=encoding, parse=parse
???encoding: 表達(dá)式轉(zhuǎn)換為字符串
???parse: 表達(dá)式轉(zhuǎn)換為邏輯值,是否作為ftl文件解析。
<#include "/common/navbar.html" parse=false encoding="Shift_JIS">
<#include "*/footer.ftl">表示當(dāng)前目錄下以及所有父目錄下的文件。
如果當(dāng)前模版在/foo/bar/template.ftl ,那么查找footer.ftl的順序為:
/foo/bar/footer.ftl
/foo/footer.ftl
/footer.ftl
這種方式講允許設(shè)計者將通用的模版放在父文件夾里面。
也可以只指定部分路徑:<#include "*/commons/footer.ftl">
五:import指令
<#import path as hash>
六:noparse
<#noparse>
? ...
</#noparse>
七:compress
<#compress>
? ...
</#compress>
八:escape,noescape
<#escape identifier as expression>
? ...
? <#noescape>...</#noescape>
? ...
</#escape>
escape指令body區(qū)的ftl的interpolations都會被自動加上escape表達(dá)式。但是不會影響字符串內(nèi)的interpolations。而且也只會影響到body內(nèi)出現(xiàn)的interpolations,比如不會影響到include的ftl的interpolations。
<#escape x as x?html>
? First name: ${firstName}
? Last name: ${lastName}
? Maiden name: ${maidenName}
</#escape>
等同于:
First name: ${firstName?html}
? Last name: ${lastName?html}
? Maiden name: ${maidenName?html}
escape指令在解析模版時起作用,而不是運(yùn)行時起作用。
escape指令還能嵌套使用,子繼承父的escape規(guī)則。
<#escape x as x?html>
? Customer Name: ${customerName}
? Items to ship:
? <#escape x as itemCodeToNameMap[x]>
??? ${itemCode1}
??? ${itemCode2}
??? ${itemCode3}
??? ${itemCode4}
? </#escape>
</#escape>
等同于:
Customer Name: ${customerName?html}
? Items to ship:
??? ${itemCodeToNameMap[itemCode1]?html}
??? ${itemCodeToNameMap[itemCode2]?html}
??? ${itemCodeToNameMap[itemCode3]?html}
??? ${itemCodeToNameMap[itemCode4]?html}
九:assign指令
<#global name=value>
or
<#global name1=value1name2=value2... nameN=valueN>
or
<#global name>
? capture this
</#global>
十一:local指令
<#local name=value>
or
<#local name1=value1name2=value2... nameN=valueN>
or
<#local name>
? capture this
</#local>
只能用于macro的定義body中。
十二:setting指令:
<#setting name=value>
name的取值范圍:
local:
number_format:
boolean_format:缺省值為"true,false"
date_format, time_format, datetime_format
time_zone:
url_escaping_charset
classic_compatible
十三:用戶自定義指令
<#macro nameparam1param2... paramN>
? ...
? <#nested loopvar1, loopvar2, ..., loopvarN>
? ...
? <#return>
? ...
</#macro>
十五:function, return
<#function nameparam1param2... paramN>
? ... <#return returnValue>
? ...
</#function>
<#function avg x y>
? <#return (x + y) / 2>
</#function>
${avg(10, 20)}?
十六:flush
<#flush>
十七:stop
<#stop>
or
<#stop reason>
取消處理模版。
十八:ftl指令
<#ftl param1=value1param2=value2...paramN=valueN>
ftl指令必須放在ftl文件的最上面。
參數(shù)范圍:
encoding:
strip_whitespace
strip_text
strict_syntax
十九:t, lt, rt
二十:attempt, recover
<#attempt>
? attempt block
<#recover> ?
recover block
</#attempt>
? Last name: ${lastName?html}
? Maiden name: ${maidenName?html}
escape指令在解析模版時起作用,而不是運(yùn)行時起作用。
escape指令還能嵌套使用,子繼承父的escape規(guī)則。
<#escape x as x?html>
? Customer Name: ${customerName}
? Items to ship:
? <#escape x as itemCodeToNameMap[x]>
??? ${itemCode1}
??? ${itemCode2}
??? ${itemCode3}
??? ${itemCode4}
? </#escape>
</#escape>
等同于:
Customer Name: ${customerName?html}
? Items to ship:
??? ${itemCodeToNameMap[itemCode1]?html}
??? ${itemCodeToNameMap[itemCode2]?html}
??? ${itemCodeToNameMap[itemCode3]?html}
??? ${itemCodeToNameMap[itemCode4]?html}
九:assign指令
<#assign name=value> or <#assign name1=value1name2=value2... nameN=valueN> or <#assign same as above... in namespacehash> or <#assign name> capture this </#assign> or <#assign name in namespacehash> capture this </#assign>十:global指令
創(chuàng)建或者替換一個已經(jīng)存在的變量,只能作用于頂層變量。 不好的做法:<#assign x>Hello ${user}!</#assign>
更改為:<#assign x="Hello ${user}!">
<#global name=value>
or
<#global name1=value1name2=value2... nameN=valueN>
or
<#global name>
? capture this
</#global>
十一:local指令
<#local name=value>
or
<#local name1=value1name2=value2... nameN=valueN>
or
<#local name>
? capture this
</#local>
只能用于macro的定義body中。
十二:setting指令:
<#setting name=value>
name的取值范圍:
local:
number_format:
boolean_format:缺省值為"true,false"
date_format, time_format, datetime_format
time_zone:
url_escaping_charset
classic_compatible
十三:用戶自定義指令
<@macro_or_transfparam1=val1param2=val2...paramN=valN/><@macro_or_transfparam1=val1param2=val2...paramN=valN ; lv1, lv2, ..., lvN/><@macro_or_transf...> ... </@macro_or_transf>十四:macro,nested,return
<@macro_or_transf...> ... </@><@macro_or_transfval1, val2, ..., valN/>
<#macro nameparam1param2... paramN>
? ...
? <#nested loopvar1, loopvar2, ..., loopvarN>
? ...
? <#return>
? ...
</#macro>
十五:function, return
<#function nameparam1param2... paramN>
? ... <#return returnValue>
? ...
</#function>
<#function avg x y>
? <#return (x + y) / 2>
</#function>
${avg(10, 20)}?
十六:flush
<#flush>
十七:stop
<#stop>
or
<#stop reason>
取消處理模版。
十八:ftl指令
<#ftl param1=value1param2=value2...paramN=valueN>
ftl指令必須放在ftl文件的最上面。
參數(shù)范圍:
encoding:
strip_whitespace
strip_text
strict_syntax
十九:t, lt, rt
二十:attempt, recover
<#attempt>
? attempt block
<#recover> ?
recover block
</#attempt>
<#attempt>
? Optional content: ${thisMayFails}
<#recover>
? Ops! The optional content is not available.
</#attempt>
? Optional content: ${thisMayFails}
<#recover>
? Ops! The optional content is not available.
</#attempt>