内置的方法:
1.
html: 特殊的 HTML 标记会被转义字符替代 (E.g. < with <)
cap_first: The string with the first letter converted to upper case
lower_case: The lowercase version of the string
upper_case: The uppercase version of the string
trim: The string without leading and trailing white-spaces
size: The number of elements in the sequence
int: The integer part of a number (e.g. -1.9?int is -1)
${test?html}
${test?upper_case?html}
test stores the string "Tom & Jerry"结果:
Tom & Jerry
TOM & JERRY
${seasons?size}
${seasons[1]?cap_first} <#-- left side can by any expression -->
${"horse"?cap_first}
seasons stores the sequence "winter", "spring", "summer", "autumn", the output will be:
4
Spring
Horse
2.
指定小数部分位数为 1: m1
指定小数位数最大为 3: M3
指定小数部分位数为 1, 但是小数位数最大可以为3: m1M3
For example, assume that x is 2.582 and y is 4:
<#-- If the language is US English the output is: -->
#{x; M2} <#-- 2.58 -->
#{y; M2} <#-- 4 -->
#{x; m1} <#-- 2.6 -->
#{y; m1} <#-- 4.0 -->
#{x; m1M2} <#-- 2.58 -->
#{y; m1M2} <#-- 4.0 -->
3.
可以指定macro中参数的默认值
you want to use "black" for color if you don't specify value for that parameter when you use the greet directive:
<#macro greet person color="black">
<font size="+2" color="${color}">Hello ${person}!</font>
</#macro>
Now <@greet person="Fred"/> is OK
现在如果color属性没有指定将使用默认值
4.
<#macro repeat count>
<#local y = "test">
<#list 1..count as x>
${y} ${count}/${x}: <#nested>
</#list>
</#macro>
<@repeat count=3>${y?default("?")} ${x?default("?")} ${count?default("?")}</@repeat>
will print this:
test 3/1: ? ? ?
test 3/2: ? ? ?
test 3/3: ? ? ?
注意<#list 1..count as x>这句的用法
5.
<#macro repeat count>
<#list 1..count as x>
<#nested x, x/2, x==count>
</#list>
</#macro>
<@repeat count=4 ; c, halfc, last>
${c}. ${halfc}<#if last> Last!</#if>
</@repeat>
The output will be:
1. 0.5
2. 1
3. 1.5
4. 2 Last!
相当于x, x/2, x==count 是 c, halfc, last的形参
这样也是可以的
<@repeat count=4 ; c, halfc, last>
${c}. ${halfc}<#if last> Last!</#if>
</@repeat>
<@repeat count=4 ; c, halfc>
${c}. ${halfc}
</@repeat>
<@repeat count=4>
Just repeat it...
</@repeat>
6.Create and replace variables with assign:
<#assign x = 1> <#-- create variable x -->
${x}
<#assign x = x + 3> <#-- replace variable x -->
${x}
Output:
1
4
7.name space
<#import "/lib/my_test.ftl" as my> <#-- hash called my will be the "gate" -->
<@my.copyright date="1999-2002"/>
${my.mail}
使用<#import "/lib/my_test.ftl" as my> 来代替<#include "/lib/my_test.ftl"> ,这样就可以利用命名空间了