设为首页 加入收藏

TOP

FreeMarker教程(二)
2014-11-24 12:02:18 来源: 作者: 【 】 浏览:136
Tags:FreeMarker 教程
whatnot
|
+- because = "don't know"
类似于目录的变量称为hashes,包含保存下级变量的唯一的查询名字
类似于文件的变量称为scalars,保存单值
scalars保存的值有两种类型:字符串(用引号括起,可以是单引号或双引号)和数字(不要用引号将数字括起,这会作为字符串处理)
对scalars的访问从root开始,各部分用“.”分隔,如animals.mouse.price
另外一种变量是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"
这种对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.
list指令

We have these animals:

NamePrice
<#list animals as being>
${being.name}${being.price} Euros
输出为:

We have these animals:

NamePrice
mouse50 Euros
elephant5000 Euros
python4999 Euros
include指令
Test page

Test page

Blah blah...

<#include "/copyright_footer.html">
一起使用指令

We have these animals:

NamePrice
<#list animals as being>
<#if being.size = "large">
${being.name}
<#if being.size = "large">
${being.price} Euros
FreeMarker设计指南(3)
--------------------------------------------------------------------------------
3、模板
(1)整体结构
l 模板使用FTL(FreeMarker模板语言)编写,是下面各部分的一个组合:
文本:直接输出
Interpolation:由${和},或#{和}来限定,计算值替代输出
FTL标记:FreeMarker指令,和HTML标记类似,名字前加#予以区分,不会输出
注释:由<#--和-->限定,不会输出
l 下面是以一个具体模板例子:
[BR]
[BR]
Welcome![BR]
[BR]
[BR]
<#-- Greet the user with his/her name -->[BR]

Welcome ${user}!

[BR]

We have these animals:[BR]

    [BR]
<#list animals as being>[BR]
  • ${being.name} for ${being.price} Euros[BR]
  • [BR]
    [BR]
    [BR]
    l [BR]是用于换行的特殊字符序列
    l 注意事项:
    FTL区分大小写,所以list是正确的FTL指令,而List不是;${name}和${NAME}是不同的
    Interpolation只能在文本中使用
    FTL标记不能位于另一个FTL标记内部,例如:
    <#if <#include 'foo'>='bar'>...
    注释可以位于FTL标记和Interpolation内部,如下面的例子:

    Welcome ${user <#-- The name of user -->}!

    [BR]

    We have these animals:[BR]

      [BR]
    <#list <#-- some comment... --> animals as <#-- again... --> being>[BR]
    ...
    多余的空白字符会在模板输出时移除
    (2)指令
    l 在FreeMarker中,使用FTL标记引用指令
    l 有三种FTL标记,这和HTML标记是类似的:
    开始标记:<#directivename parameters>
    结束标记:
    空内容指令标记:<#directivename parameters/>
    l 有两种类型的指令:预定义指令和用户定义指令
    l 用户定义指令要使用@替换#,如<@mydirective>...(会在后面讲述)
    l FTL标记不能够交叉,而应该正确的嵌套,如下面的代码是错误的:
    <#list animals as being>
  • ${being.name} for ${being.price} Euros
  • <#if use = "Big Joe">
    (except for you)
    <#-- WRONG! -->
    l 如果使用不存在的指令,FreeMarker不会使用模板输出,而是产生一个错误消息
    l FreeMarker会忽略FTL标记中的空白字符,如下面的例子:
    <#list[BR]
    animals as[BR]
    bei
    首页 上一页 1 2 3 4 5 6 下一页 尾页 2/6/6
    】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
    分享到: 
    上一篇freemaker的初步使用 下一篇java关于集合的遍历与增强for循环..

    评论

    帐  号: 密码: (新用户注册)
    验 证 码:
    表  情:
    内  容: