First name: ${firstName
?html}
Last name: ${lastName
?html}
Maiden name: ${maidenName
?html}
escape指令在解析模版时起作用,而不是运行时起作用。
escape指令还能嵌套使用,子继承父的escape规则。
<#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>
创建或者替换一个已经存在的变量,只能作用于顶层变量。
不好的做法:<#assign x>Hello ${user}!</#assign>
更改为:<#assign x="Hello ${user}!">
十:global指令
<#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_or_transf...>
...
</@><@macro_or_transfval1, val2, ..., valN/>
十四:macro,nested,return
<#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文件的最上面。
参数范围:
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>