| +- animals | | | +- 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:
We have these animals:
Test page
Blah blah... <#include \ ? 一起使用指令
We have these animals:
(1)整体结构
? 模板使用FTL(FreeMarker模板语言)编写,是下面各部分的一个组合:
? 文本:直接输出
? Interpolation:由${和},或#{和}来限定,计算值替代输出
? FTL标记:FreeMarker指令,和HTML标记类似,名字前加#予以区分,不会
输出
? 注释:由<#--和-->限定,不会输出 ? 下面是以一个具体模板例子:
[BR]
[BR]Welcome ${user}!
[BR]We have these animals:[BR] [BR] <#list animals as being>[BR] ${being.name} for ${being.price} Euros[BR] #list>[BR] [BR] [BR]
? [BR]是用于换行的特殊字符序列 ? 注意事项:
? FTL区分大小写,所以list是正确的FTL指令,而List不是;${name}和
${NAME}是不同的
? Interpolation只能在文本中使用
? FTL标记不能位于另一个FTL标记内部,例如:
<#if <#include 'foo'>='bar'>... ? 注释可以位于FTL标记和Interpolation内部,如下面的例子:
Welcome ${user <#-- The name of user -->}!
[BR]下一篇:湖北蓝田股份有限公司财务报表分析