1、指令
1.1、if指令
和Java类似,if指令是一个分支控制指令; 参考之前的数据模型,直接写例子:
先写个简单点的,只带if
<#if animals.mouse.price == 50>
the price of mouse is 50
</#if>
假如运算符号是>或>= ,FreeMarker会把>看成是指定结束标签的符号,这时候应该用括号把使用这些符号的地方括起来。比如<#if (animals.mouse.price == 50) >
加上else
<#if animals.mouse.price < animals.elephant.price>
mouse is cheaper than elephant
<#else>
elephant is cheaper than mouse
</#if>
加上elseif
<#if animals.mouse.price < animals.elephant.price>
mouse is cheaper than elephant
<#elseif animals.elephant.price < animals.python.price>
elephant is cheaper than mouse
<#else>
elephant and mouse cost the same
</#if>
假如变量是布尔值
<#if animals.elephant.protected>
elephant are protected animals!
</#if>
1.2、list指令
显示列表内容 格式:
<#list a as b>c</#list>
<p>we have these animals:</p>
<ul>
<#list animals as being>
<li>${being.name} for ${being.price} Euros</li>
</#list>
</ul>
输出结果:
<p>we have these animals:</p>
<ul>
<li>mouse for 50 Euros</li>
<li>elephant for 5000 Euros</li>
<li>python for 4999 Euros</li>
</ul>
参考上面的数据模型2,还有个栗子:
<#list misc.fruits>
<p>Fruits:
<ul>
<#items as fruit>
<li>${fruit}<#sep> and</#sep>
</#items>
</ul>
<#else>
<p>We have no fruits.
</#list>
里面有2个指令items和sep:如果不得不在第一列表项之前或在最后一个列表项之后打印一些东西, 那么就要使用 items 指令,但至少要有一项;假如有0个水果,会输出一个空的 <ul></ul>,而不是什么都没有。<#sep>起了分割作用。
1.3、include指令
可以在模板中插入其他文件的内容,当以后修改了这个文件,访问者在所有页面都会看到新的内容
比如有个文件 footer.html
<hr>
<i>
Copyright (c) 2000 <a href="http://www.abc.com">Abc Inc</a>,
<br>
All Rights Reserved.
</i>
需要使用这个文件时,可以用include指令插入
<html>
<head>
<title>Test page</title>
</head>
<body>
<h1>Test page</h1>
<#include "/footer.html">
</body>
</html>
输出的内容:
<html>
<head>
<title>Test page</title>
</head>
<body>
<h1>Test page</h1>
<hr>
<i>
Copyright (c) 2000 <a href="http://www.abc.com">Abc Inc</a>,
<br>
All Rights Reserved.
</i>
</body>
</html>
1.4、import指令
该指令用于导入FreeMarker模板中的所有变量,并将该变量放置在指定的Map对象中
<#import "/lib/common.ftl" as com>
例子是导入/lib/common.ftl模板文件的所有变量,将这些变量放在一个名为com的Map对象中;
##1.5、noparse指令 指定freemarker不处理该指令里包含的内容,即输出里面的原代码
例子:
<#noparse>
<#list books as book>
<tr><td>${book.name}<td>作者:${book.author}
</#list>
</#noparse>
输出:
<#list books as book>
<tr><td>${book.name}<td>作者:${book.author}
</#list>
1.6、escape和noescape指令
escape指令会使body区的插值都会被自动加上escape表达式,但不会影响字符串内的插值;如果需要指定escape指令中某些插值无需添加escape表达式,则应该使用noescape指令
格式如下:
<#escape identifier as expression>...
<#noescape>...</#noescape>
</#escape>
举个栗子:
<#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指令在解析模板时起作用而不是在运行时起作用,除此之外,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}
1.7、assign指令
可以为模板页面创建获替换一个或多个顶层变量;
格式如下:
<#assign name=value [in namespacehash]>
name是变量的名字,value是值,in字句可以将创建的变量放入namespacesh命名空间中;
例子:
<#assign x="Hello ${user}!">
1.8、setting指令
该指令用于设置FreeMarker的运行环境。
该指令的语法格式如下:
<#setting name=value><#setting name=value>
name的取值范围如下:
- locale:该选项指定该模板所用的国家/语言选项
- number_format:指定格式化输出数字的格式
- boolean_format:指定两个布尔值的语法格式,默认值是true,false
- date_format,time_format,datetime_format:指定格式化输出日期的格式
- time_zone:设置格式化输出日期时所使用的时区
1.9、macro、neated、return指令
macro可以用于实现自定义指令,通过使用自定义指令,可以将一段模板片段定义成一个用户指令,使用macro指令的语法格式如下:
<#macro name param1 param2 ... paramN>
...
<#nested loopvar1, loopvar2, ..., loopvarN>
...
<#return>
...
</#macro>
介绍下如下几个部分:
- name:指定的是该自定义指令的名字,使用自定义指令时可以传入多个参数
- paramX:参数,若用自定义指令时,必须为这些参数传入值
- nested指令:nested标签输出使用自定义指令时的中间部分
- nested指令中的循环变量:将由macro定义部分指定,传给使用标签的模板
- return指令:用于随时结束该自定义指令.
例子:
<#macro book> //定义一个自定义指令
j2ee
</#macro>
<@book /> //使用定义的指令
输出:j2ee
在上面的代码中,可能很难看出自定义标签的用处,因为我们定义的book指令所包含的内容非常简单。实际上,自定义标签可包含非常多的内容,从而可以实现更好的代码复用。此外,还可以在定义自定义指令时,为自定义指令指定参数。
<#macro book booklist> //定义一个自定义指令booklist是参数
<#list booklist as book>
${book}
</#list>
</#macro>
<@book booklist=["spring","j2ee"] /> //使用刚刚定义的指令
输出:spring j2ee
2、表达式
通常喜欢使用直接确定的值,而不是计算得到的结果
2.1、字符串
用双引号或单引号判断字符串值。假如文本自身包含单、双引号或斜杠时,应使用转义字符反斜杠 \
${"It's \"quoted\" and this is a backslash: \\"}
${'It\'s "quoted" and this is a backslash: \\'}
输出:
It's "quoted" and this is a backslash: \
It's "quoted" and this is a backslash: \
FreeMarker支持的所有转义字符
" 引号 (u0022)
' 单引号(又称为撇号) (u0027)
{ 起始花括号:{
\ 反斜杠 (u005C)
\n 换行符 (u000A)
\r 回车 (u000D)
\t 水平制表符(又称为tab) (u0009)
\b 退格 (u0008)
\f 换页 (u000C)
\l 小于号:<
\g 大于号:>
\a &符:&
\xCode 字符的16进制 Unicode 码 (UCS 码)
因为${ 和 #{ 有着特殊的含义,想打印他们,就要使用原生字符串,或者进行转义。原生字符串中${ 和反斜杠没有特殊的含义,都是普通的字符。为了表明字符串是原生字符串,需要在开始的引号之前加字母 r
${r"${foo}"}
${r"C:\foo\bar"}
输出
${foo}
C:\foo\bar
2.2、数字
输入不带引号的数字就是数字,可以用小数点和正负号,不能用科学计数法;
2.3、布尔值
布尔值直接用true或false表示,也不需要引号;
2.4、序列
指定一个文字的序列,用逗号分隔其中每个子变量,然后把这个列表放方括号里。
e.g
<#list ["spring", "summer", "autumn", "winter"] as x>
${x}
</#list>
输出
spring
summer
autumn
winter
列表中表达式还可以写成:[2+2,[1,2,3,4],"what"],其中第一个子变量是4;第二个是一个序列,这个序列也可写成[1..4];第三个是一个字符串
2.5、哈希表
在模板中指定一个哈希表,可以遍历其中的键值对,如{"name":"red mouse","price":100}
3、字符串操作
3.1、插值
在字符串中插入表达式的值,用${}或#{};
假如用户是world,则
${"hello ${user}!"}
输出
Helloworld!
上面的也可以这样写:
${"hello" + user + "!"}
输出的结果和上面一样;
插值只能在文本字段和字符串文字中使用;
3.2、获取一个字符
用索引,${user[1]}输出 o
4、序列操作
4.1、连接
序列中的连接可以用+号搞定;比如:
<# list ["spring"," summer"] + ["autumn", "winter"] as x>
-${x}
</#list>
输出
-spring
-summer
-autumn
-winter
4.2、切分
用索引 [1..4]输出4项,[1..]输出第二项之后的所有项;
5、哈希表连接
不同的哈希表也可以用+号连接,若两个哈希表的有键相同的项,+右边的哈希表中的项目优先
<#assign ages = {"Joe":23, "Fred":25} + {"Joe":30, "Julia":18}>
- Joe is ${ages.Joe}
- Fred is ${ages.Fred}
- Julia is ${ages.Julia}
输出
- Joe is 30
- Fred is 25
- Julia is 18
6、内建函数
6.1、字符串
- html:字符串中所有的特殊html字符都要用实体引用代替;
- cap_first:第一个字母变大写
- lower_case:字符串变小写
- upper_case:字符串变大写
- trim:去掉字符串首尾的空格
栗子:假如字符串text存储的是“abc & def"
${text?html}
${text?upper_case?html}<#-- 内嵌函数双重使用 -->
输出:
abc & def
ABC & DEF
6.2、序列
size:序列中元素的个数
6.3、数字
int:数字取整,例如 -1.9?int 是-1
7、方法调用
方法调用操作来使用一个已经定义过的方法,语法形式是用逗号来分割在括号内表达式形成的的参数列表。
例如:定义了一个方法repeat,第一个参数是字符串,第二个是数值,方法返回值是字符串类型。
${repeat("what", 3)}
输出
whatwhatwhat
8、处理不存在的值
8.1、默认值
使用形式: unsafe_expr!default_expr 或 unsafe_expr! or (unsafe_expr)!default_expr 或 (unsafe_expr)!
这个操作符允许你为可能不存在的变量指定一个默认值;
例子:
${mouse!"No mouse."}
<#assign mouse="Jerry">
${mouse!"No mouse."}
输出
No mouse.
Jerry
默认值可以是任何类型的表达式,若被省略,结果会是空串、空序列或空哈希表;若想让默认值为0或false,则注意不能省略它。
(${mouse!})
<#assign mouse = "Jerry">
(${mouse!})
输出
()
(Jerry)
8.2、检测不存在的值
格式: unsafe_expr?? 或 (unsafe_expr)??
例子:假如mouse变量不存在
<#if mouse??>
Mouse found
<#else>
No mouse found
</#if>
Creating mouse...
<#assign mouse = "Jerry">
<#if mouse??>
Mouse found
<#else>
No mouse found
</#if>
输出
No mouse found
Creating mouse...
Mouse found