| +- mouse | | |
| | +- size = \ | | | | | +- price = 50 | |
| +- elephant | | |
| | +- size = \ | | |
| | +- price = 5000 | | | +- python | |
| +- size = \ | |
| +- price = 4999 |
+- test = \ | +- whatnot |
+- because = \
? 类似于目录的变量称为hashes,包含保存下级变量的唯一的查询名字 ? 类似于文件的变量称为scalars,保存单值
? scalars保存的值有两种类型:字符串(用引号括起,可以是单引号或双引
号)和数字(不要用引号将数字括起,这会作为字符串处理) ? 对scalars的访问从root开始,各部分用“.”分隔,如animals.mouse.price ? 另外一种变量是sequences,和hashes类似,只是不使用变量名字,而使
用数字索引,如下面的例子:
(root) |
+- animals | | | +- (1st) | | |
| | +- name = \ | | |
| | +- size = \ | | |
| | +- price = 50 | | | +- (2nd) | | |
| | +- name = \ | | |
| | +- size = \ | | |
| | +- price = 5000 | | | +- (3rd) | |
| +- name = \ | |
| +- size = \ | |
| +- price = 4999 | +- whatnot | +- fruits |
+- (1st) = \ |
+- (2nd) = \
? 这种对scalars的访问使用索引,如animals[0].name (3)模板
? 在FreeMarker模板中可以包括下面三种特定部分:
? ${?}:称为interpolations,FreeMarker会在输出时用实际值进行替
代
? FTL标记(FreeMarker模板语言标记):类似于HTML标记,为了与HTML
标记区分,用#开始(有些以@开始,在后面叙述) ? 注释:包含在<#--和-->(而不是)之间 ? 下面是一些使用指令的例子:
? if指令
<#if animals.python.price < animals.elephant.price> Pythons are cheaper than elephants today. <#else>
Pythons are not cheaper than elephants today. #if>
? list指令
We have these animals:
${being.name}${being.price} Euros #list>
输出为:
We have these animals:
? include指令
Test page
Blah blah...
<#include \
? 一起使用指令
We have these animals:
<#if being.size = \ ${being.name}
<#if being.size = \ ${being.price} Euros #list>
2、数据模型 (1)基础
? 在快速入门中介绍了在模板中使用的三种基本对象类型:scalars、hashes
和sequences,其实还可以有其它更多的能力: ? scalars:存储单值
? hashes:充当其它对象的容器,每个都关联一个唯一的查询名字 ? sequences:充当其它对象的容器,按次序访问
? 方法:通过传递的参数进行计算,以新对象返回结果
? 用户自定义FTL标记:宏和变换器
? 通常每个变量只具有上述的一种能力,但一个变量可以具有多个上述能力,
如下面的例子:
(root) |
+- mouse = \ |
+- age = 12 |
+- color = \
? mouse既是scalars又是hashes,将上面的数据模型合并到下面的模板:
${mouse} <#-- use mouse as scalar --> ${mouse.age} <#-- use mouse as hash --> ${mouse.color} <#-- use mouse as hash -->
? 输出结果是:
Yerri 12 brown
(2)Scalar变量
? Scalar变量存储单值,可以是:
? 字符串:简单文本,在模板中使用引号(单引号或双引号)括起 ? 数字:在模板中直接使用数字值
? 日期:存储日期/时间相关的数据,可以是日期、时间或日期-时间
(Timestamp);通常情况,日期值由程序员加到数据模型中,设计者只需要显示它们
? 布尔值:true或false,通常在<#if ?>标记中使用 (3)hashes 、sequences和集合
? 有些变量不包含任何可显示的内容,而是作为容器包含其它变量,者有两
种类型:
? hashes:具有一个唯一的查询名字和它包含的每个变量相关联
? sequences:使用数字和它包含的每个变量相关联,索引值从0开始 ? 集合变量通常类似sequences,除非无法访问它的大小和不能使用索引来获
得它的子变量;集合可以看作只能由<#list ?>指令使用的受限sequences (4)方法
? 方法变量通常是基于给出的参数计算值
? 下面的例子假设程序员已经将方法变量avg放到数据模型中,用来计算数
字平均值:
The average of 3 and 5 is: ${avg(3, 5)}