1、快速入门
(1)模板 + 数据模型 = 输出
l FreeMarker基于设计者和程序员是具有不同专业技能的不同个体的观念
l 他们是分工劳动的:设计者专注于表示——创建HTML文件、图片、Web页面的其它可视化方面;程序员创建系统,生成设计页面要显示的数据
l 经常会遇到的问题是:在Web页面(或其它类型的文档)中显示的信息在设计页面时是无效的,是基于动态数据的
l 在这里,你可以在HTML(或其它要输出的文本)中加入一些特定指令,FreeMarker会在输出页面给最终用户时,用适当的数据替代这些代码
l 下面是一个例子:
<html>
<head>
<title>Welcome!</title>
</head>
<body>
<h1>Welcome ${user}!</h1>
<p>Our latest product:
<a href="${latestProduct.url}">${latestProduct.name}</a>!
</body>
</html>
l 这个例子是在简单的HTML中加入了一些由${…}包围的特定代码,这些特定代码是FreeMarker的指令,而包含FreeMarker的指令的文件就称为模板(Template)
l 至于user、latestProduct.url和latestProduct.name来自于数据模型(data model)
l 数据模型由程序员编程来创建,向模板提供变化的信息,这些信息来自于数据库、文件,甚至于在程序中直接生成
l 模板设计者不关心数据从那儿来,只知道使用已经建立的数据模型
l 下面是一个可能的数据模型:
(root)
|
+- user = "Big Joe"
|
+- latestProduct
|
+- url = "products/greenmouse.html"
|
+- name = "green mouse"
l 数据模型类似于计算机的文件系统,latestProduct可以看作是目录,而user、url和name看作是文件,url和name文件位于latestProduct目录中(这只是一个比喻,实际并不存在)
l 当FreeMarker将上面的数据模型合并到模板中,就创建了下面的输出:
<html>
<head>
<title>Welcome!</title>
</head>
<body>
<h1>Welcome Big Joe!</h1>
<p>Our latest product:
<a href="products/greenmouse.html">green mouse</a>!
</body>
</html>
(2)数据模型
l 典型的数据模型是树型结构,可以任意复杂和深层次,如下面的例子:
(root)
|
+- animals
| |
| +- mouse
| | |
| | +- size = "small"
| | |
| | +- price = 50
| |
| +- elephant
| | |
| | +- size = "large"
| | |
| | +- price = 5000
| |
| +- python
| |
| +- size = "medium"
| |
| +- price = 4999
|
+- test = "It is a test"
|
+- whatnot
|
+- because = "don't know"
l 类似于目录的变量称为hashes,包含保存下级变量的唯一的查询名字
l 类似于文件的变量称为scalars,保存单值
l scalars保存的值有两种类型:字符串(用引号括起,可以是单引号或双引号)和数字(不要用引号将数字括起,这会作为字符串处理)
l 对scalars的访问从root开始,各部分用“.”分隔,如animals.mouse.price
l 另外一种变量是sequences,和hashes类似,只是不使用变量名字,而使用数字索引,如下面的例子:
(root)
|
+- animals
| |
| +- (1st)
| | |
| | +- name = "mouse"
| | |
| | +- size = "small"
| | |
| | +- price = 50
| |
| +- (2nd)
| | |
| | +- name = "elephant"
| | |
| | +- size = "large"
| | |
| | +- price = 5000
| |
| +- (3rd)
| |
| +- name = "python"
| |
| +- size = "medium"
| |
| +- price = 4999
|
+- whatnot
|
+- fruits
|
+- (1st) = "orange"
|
+- (2nd) = "banana"
l 这种对scalars的访问使用索引,如animals[0].name
(3)模板
l 在FreeMarker模板中可以包括下面三种特定部分:
Ø ${…}:称为interpolations,FreeMarker会在输出时用实际值进行替代
Ø FTL标记(FreeMarker模板语言标记):类似于HTML标记,为了与HTML标记区分,用#开始(有些以@开始,在后面叙述)
Ø 注释:包含在<#--和-->(而不是<!--和-->)之间
l 下面是一些使用指令的例子:
Ø if指令
<#if animals.python.price < animals.elephant.price>
Pythons are cheaper than elephants today.
<#else>
Pythons are not cheaper than elephants today.
</#if>
Ø list指令
<p>We have these animals:
<table border=1>
<tr><th>Name<th>Price
<#list animals as being>
<tr><td>${being.name}<td>${being.price} Euros
</#list>
</table>
输出为:
<p>We have these animals:
<table border=1>
<tr><th>Name<th>Price
<tr><td>mouse<td>50 Euros
<tr><td>elephant<td>5000 Euros
<tr><td>python<td>4999 Euros
</table>
Ø include指令
<html>
<head>
<title>Test page</title>
</head>
<body>
<h1>Test page</h1>
<p>Blah blah...
<#include "/copyright_footer.html">
</body>
</html>
Ø 一起使用指令
<p>We have these animals:
<table border=1>
<tr><th>Name<th>Price
<#list animals as being>
<tr>
<td>
<#if being.size = "large"><b></#if>
${being.name}
<#if being.size = "large"></b></#if>
<td>${being.price} Euros
</#list>
</table>
(root)|+- mouse = "Yerri"|+- age = 12|+- color = "brown">${mouse} <#-- use mouse as scalar -->${mouse.age} <#-- use mouse as hash -->${mouse.color} <#-- use mouse as hash -->Yerri12brownThe average of 3 and 5 is: ${avg(3, 5)}The average of 6 and 10 and 20 is: ${avg(6, 10, 20)}The average of the price of python and elephant is: ${avg(animals.python.price, animals.elephant.price)}
3、模板
(1)整体结构
l 模板使用FTL(FreeMarker模板语言)编写,是下面各部分的一个组合:
Ø 文本:直接输出
Ø Interpolation:由${和},或#{和}来限定,计算值替代输出
Ø FTL标记:FreeMarker指令,和HTML标记类似,名字前加#予以区分,不会输出
Ø 注释:由<#--和-->限定,不会输出
l 下面是以一个具体模板例子:
<html>[BR]<head>[BR]<title>Welcome!</title>[BR]</head>[BR]<body>[BR]<#-- Greet the user with his/her name -->[BR]<h1>Welcome ${user}!</h1>[BR]<p>We have these animals:[BR]<ul>[BR]<#list animals as being>[BR]<li>${being.name} for ${being.price} Euros[BR]</#list>[BR]</ul>[BR]</body>[BR]</html>l [BR]是用于换行的特殊字符序列
l 注意事项:
Ø FTL区分大小写,所以list是正确的FTL指令,而List不是;${name}和${NAME}是不同的
Ø Interpolation只能在文本中使用
Ø FTL标记不能位于另一个FTL标记内部,例如:
<#if <#include 'foo'>='bar'>...</if>Ø 注释可以位于FTL标记和Interpolation内部,如下面的例子:
<h1>Welcome ${user <#-- The name of user -->}!</h1>[BR]<p>We have these animals:[BR]<ul>[BR]<#list <#-- some comment... --> animals as <#-- again... --> being>[BR]...Ø 多余的空白字符会在模板输出时移除
(2)指令
l 在FreeMarker中,使用FTL标记引用指令
l 有三种FTL标记,这和HTML标记是类似的:
Ø 开始标记:<#directivename parameters>
Ø 结束标记:</#directivename>
Ø 空内容指令标记:<#directivename parameters/>
l 有两种类型的指令:预定义指令和用户定义指令
l 用户定义指令要使用@替换#,如<@mydirective>...</@mydirective>(会在后面讲述)
l FTL标记不能够交叉,而应该正确的嵌套,如下面的代码是错误的:
<ul><#list animals as being><li>${being.name} for ${being.price} Euros<#if use = "Big Joe">(except for you)</#list></#if> <#-- WRONG! --></ul>l 如果使用不存在的指令,FreeMarker不会使用模板输出,而是产生一个错误消息
l FreeMarker会忽略FTL标记中的空白字符,如下面的例子:
<#list[BR]animals as[BR]being[BR]>[BR]${being.name} for ${being.price} Euros[BR]</#list >l 但是,<、</和指令之间不允许有空白字符
(3)表达式
l 直接指定值
Ø 字符串
n 使用单引号或双引号限定
n 如果包含特殊字符需要转义,如下面的例子:
${"It's /"quoted/" andthis is a backslash: //"}${'It/'s "quoted" andthis is a backslash: //'}输出结果是:
It's "quoted" andthis is a backslash: /It's "quoted" andthis is a backslash: /n 下面是支持的转义序列:
转义序列
含义
/"
双引号(u0022)
/'
单引号(u0027)
//
反斜杠(u005C)
/n
换行(u000A)
/r
Return (u000D)
/t
Tab (u0009)
/b
Backspace (u0008)
/f
Form feed (u000C)
/l
<
/g
>
/a
&
/{
{
/xCode
4位16进制Unicode代码
n 有一类特殊的字符串称为raw字符串,被认为是纯文本,其中的/和{等不具有特殊含义,该类字符串在引号前面加r,下面是一个例子:
${r"${foo}"}${r"C:/foo/bar"}输出的结果是:
${foo}C:/foo/barØ 数字
n 直接输入,不需要引号
n 精度数字使用“.”分隔,不能使用分组符号
n 目前版本不支持科学计数法,所以“1E3”是错误的
n 不能省略小数点前面的0,所以“.5”是错误的
n 数字8、+8、08和8.00都是相同的
Ø 布尔值
n true和false,不使用引号
Ø 序列
n 由逗号分隔的子变量列表,由方括号限定,下面是一个例子:
<#list ["winter", "spring", "summer", "autumn"] as x>${x}</#list>输出的结果是:
winterspringsummerautumnn 列表的项目是表达式,所以可以有下面的例子:
[2 + 2, [1, 2, 3, 4], "whatnot"]n 可以使用数字范围定义数字序列,例如2..5等同于[2, 3, 4, 5],但是更有效率,注意数字范围没有方括号
n 可以定义反递增的数字范围,如5..2
Ø 散列(hash)
n 由逗号分隔的键/值列表,由大括号限定,键和值之间用冒号分隔,下面是一个例子:
{"name":"green mouse", "price":150}n 键和值都是表达式,但是键必须是字符串
l 获取变量
Ø 顶层变量: ${variable},变量名只能是字母、数字、下划线、$、@和#的组合,且不能以数字开头
Ø 从散列中获取数据
n 可以使用点语法或方括号语法,假设有下面的数据模型:
(root)|+- book| || +- title = "Breeding green mouses"| || +- author| || +- name = "Julia Smith"| || +- info = "Biologist, 1923-1985, Canada"|+- test = "title"下面都是等价的:
book.author.namebook["author"].namebook.author.["name"]book["author"]["name"]n 使用点语法,变量名字有顶层变量一样的限制,但方括号语法没有该限制,因为名字是任意表达式的结果
Ø 从序列获得数据:和散列的方括号语法语法一样,只是方括号中的表达式值必须是数字;注意:第一个项目的索引是0
Ø 序列片断:使用[startIndex..endIndex]语法,从序列中获得序列片断(也是序列);startIndex和endIndex是结果为数字的表达式
Ø 特殊变量:FreeMarker内定义变量,使用.variablename语法访问
${"Hello ${user}!"}${"${user}${user}${user}${user}"}${"Hello " + user + "!"}${user + user + user + user}<#if ${isBig}>Wow!</#if><#if "${isBig}">Wow!</#if><#if isBig>Wow!</#if>${user[0]}${user[4]}${user[1..4]}BJig J<#list ["Joe", "Fred"] + ["Julia", "Kate"] as user>- ${user}</#list>- Joe- Fred- Julia- Kate<#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${x * x - 100}${x / 2}${12 % 10}-752.52${3 * "5"} <#-- WRONG! -->${3 + "5"}35${(x/2)?int}${1.1?int}${1.999?int}${-1.1?int}${-1.999?int}211-1-1<#if x < 12 && color = "green">We have less than 12 things, and they are green.</#if><#if !hot> <#-- here hot must be a boolean -->It's not hot.</#if>${test?html}${test?upper_case?html}Tom & JerryTOM & JERRY
操作符组
操作符
后缀
[subvarName] [subStringRange] . (methodParams)
一元
+expr、-expr、!
内建
?
乘法
*、 / 、%
加法
+、-
关系
<、>、<=、>=(lt、lte、gt、gte)
相等
==(=)、!=
逻辑and
&&
逻辑or
||
数字范围
..
<#setting number_format="currency"/><#assign answer=42/>${answer}${answer?string} <#-- the same as ${answer} -->${answer?string.number}${answer?string.currency}${answer?string.percent}$42.00$42.0042$42.004,200%${lastUpdated?string("yyyy-MM-dd HH:mm:ss zzzz")}${lastUpdated?string("EEE, MMM d, ''yy")}${lastUpdated?string("EEEE, MMMM dd, yyyy, hh:mm:ss a '('zzz')'")}2003-04-08 21:24:44 Pacific Daylight TimeTue, Apr 8, '03Tuesday, April 08, 2003, 09:24:44 PM (PDT)<#assign foo=true/>${foo?string("yes", "no")}yes<#-- If the language is US English the output is: --><#assign x=2.582/><#assign y=4/>#{x; M2} <#-- 2.58 -->#{y; M2} <#-- 4 -->#{x; m1} <#-- 2.6 -->#{y; m1} <#-- 4.0 -->#{x; m1M2} <#-- 2.58 -->#{y; m1M2} <#-- 4.0 -->
<#macro greet><font size="+2">Hello Joe!</font></#macro><@greet></@greet><@greet/><#macro greet person><font size="+2">Hello ${person}!</font></#macro><@greet person="Fred"/> and <@greet person="Batman"/><font size="+2">Hello Fred!</font>and <font size="+2">Hello Batman!</font><@greet person=Fred/><#macro greet person color><font size="+2" color="${color}">Hello ${person}!</font></#macro><@greet person="Fred" color="black"/><@greet color="black" person="Fred"/><@greet person="Fred" color="black" background="green"/><@greet person="Fred"/><#macro greet person color="black"><font size="+2" color="${color}">Hello ${person}!</font></#macro><#macro border><table border=4 cellspacing=0 cellpadding=4><tr><td><#nested></tr></td></table></#macro><@border>The bordered text</@border><table border=4 cellspacing=0 cellpadding=4><tr><td>The bordered text</tr></td></table><#macro do_thrice><#nested><#nested><#nested></#macro><@do_thrice>Anything.</@do_thrice>Anything.Anything.Anything.<@border><ul><@do_thrice><li><@greet person="Joe"/></@do_thrice></ul></@border><table border=4 cellspacing=0 cellpadding=4><tr><td><ul><li><font size="+2">Hello Joe!</font><li><font size="+2">Hello Joe!</font><li><font size="+2">Hello Joe!</font></ul></tr></td></table><#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>test 3/1: ? ? ?test 3/2: ? ? ?test 3/3: ? ? ?<#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>1. 0.52. 13. 1.54. 2 Last!(2)在模板中定义变量
l 在模板中定义的变量有三种类型:
Ø plain变量:可以在模板的任何地方访问,包括使用include指令插入的模板,使用assign指令创建和替换
Ø 局部变量:在宏定义体中有效,使用local指令创建和替换
Ø 循环变量:只能存在于指令的嵌套内容,由指令(如list)自动创建;宏的参数是局部变量,而不是循环变量
l 局部变量隐藏(而不是覆盖)同名的plain变量;循环变量隐藏同名的局部变量和plain变量,下面是一个例子:
<#assign x = "plain">1. ${x} <#-- we see the plain var. here --><@test/>6. ${x} <#-- the value of plain var. was not changed --><#list ["loop"] as x>7. ${x} <#-- now the loop var. hides the plain var. --><#assign x = "plain2"> <#-- replace the plain var, hiding does not mater here -->8. ${x} <#-- it still hides the plain var. --></#list>9. ${x} <#-- the new value of plain var. --><#macro test>2. ${x} <#-- we still see the plain var. here --><#local x = "local">3. ${x} <#-- now the local var. hides it --><#list ["loop"] as x>4. ${x} <#-- now the loop var. hides the local var. --></#list>5. ${x} <#-- now we see the local var. again --></#macro>1. plain2. plain3. local4. loop5. local6. plain7. loop8. loop9. plain2l 内部循环变量隐藏同名的外部循环变量,如:
<#list ["loop 1"] as x>${x}<#list ["loop 2"] as x>${x}<#list ["loop 3"] as x>${x}</#list>${x}</#list>${x}</#list>loop 1loop 2loop 3loop 2loop 1l 模板中的变量会隐藏(而不是覆盖)数据模型中同名变量,如果需要访问数据模型中的同名变量,使用特殊变量global,下面的例子假设数据模型中的user的值是Big Joe:
<#assign user = "Joe Hider">${user} <#-- prints: Joe Hider -->${.globals.user} <#-- prints: Big Joe -->(3)名字空间
l 通常情况,只使用一个名字空间,称为主名字空间
l 为了创建可重用的宏、变换器或其它变量的集合(通常称库),必须使用多名字空间,其目的是防止同名冲突
l 创建库
Ø 下面是一个创建库的例子(假设保存在lib/my_test.ftl中):
<#macro copyright date><p>Copyright (C) ${date} Julia Smith. All rights reserved.<br>Email: ${mail}</p></#macro><#assign mail = "jsmith@acme.com">Ø 使用import指令导入库到模板中,Freemarker会为导入的库创建新的名字空间,并可以通过import指令中指定的散列变量访问库中的变量:
<#import "/lib/my_test.ftl" as my><#assign mail="fred@acme.com"><@my.copyright date="1999-2002"/>${my.mail}${mail}输出结果:
<p>Copyright (C) 1999-2002 Julia Smith. All rights reserved.<br>Email: jsmith@acme.com</p>jsmith@acme.comfred@acme.com可以看到例子中使用的两个同名变量并没有冲突,因为它们位于不同的名字空间
l 可以使用assign指令在导入的名字空间中创建或替代变量,下面是一个例子:
<#import "/lib/my_test.ftl" as my>${my.mail}<#assign mail="jsmith@other.com" in my>${my.mail}l 输出结果:
jsmith@acme.comjsmith@other.coml 数据模型中的变量任何地方都可见,也包括不同的名字空间,下面是修改的库:
<#macro copyright date><p>Copyright (C) ${date} ${user}. All rights reserved.</p></#macro><#assign mail = "${user}@acme.com">l 假设数据模型中的user变量的值是Fred,则下面的代码:
<#import "/lib/my_test.ftl" as my><@my.copyright date="1999-2002"/>${my.mail}l 输出结果:
<p>Copyright (C) 1999-2002 Fred. All rights reserved.</p>Fred@acme.com
转自: http://blog.youkuaiyun.com/chenyun2000/category/60121.aspx
本文介绍 FreeMarker 模板引擎的基本概念,包括数据模型、模板结构和指令使用等核心内容,帮助开发者快速掌握 FreeMarker 的应用。
474

被折叠的 条评论
为什么被折叠?



