ITPub博客

首页 > 应用开发 > IT综合 > FreeMarker设计指南(1)

FreeMarker设计指南(1)

原创 IT综合 作者:okone96 时间:2007-03-16 17:58:15 0 删除 编辑

http://www.chinascripts.com/tech/java/opensource/
template/2006-10-30/9a8a44c04a09d111.html

1、快速入门

(1)模板 + 数据模型 = 输出

FreeMarker基于设计者和程序员是具有不同专业技能的不同个体的观念

他们是分工劳动的:设计者专注于表示——创建HTML文件、图片、Web页面的其它可视化方面;程序员

创建系统,生成设计页面要显示的数据

经常会遇到的问题是:在Web页面(或其它类型的文档)中显示的信息在设计页面时是无效的,是基于动


态数据的

在这里,你可以在HTML(或其它要输出的文本)中加入一些特定指令,FreeMarker会在输出页面给最终

用户时,用适当的数据替代这些代码

下面是一个例子:



Welcome!


Welcome ${user}!


Our latest product:
${latestProduct.name}!


这个例子是在简单的HTML中加入了一些由${…}包围的特定代码,这些特定代码是FreeMarker的指令,而

包含FreeMarker的指令的文件就称为模板(Template)

至于user、latestProduct.url和latestProduct.name来自于数据模型(data model)

数据模型由程序员编程来创建,向模板提供变化的信息,这些信息来自于数据库、文件,甚至于在程序中

直接生成

模板设计者不关心数据从那儿来,只知道使用已经建立的数据模型

下面是一个可能的数据模型:

(root)
|
+- user = "Big Joe"
|
+- latestProduct
|
+- url = "products/greenmouse.html"
|
+- name = "green mouse"
数据模型类似于计算机的文件系统,latestProduct可以看作是目录,而user、url和name看作是文件,

url和name文件位于latestProduct目录中(这只是一个比喻,实际并不存在)

当FreeMarker将上面的数据模型合并到模板中,就创建了下面的输出:



Welcome!


Welcome Big Joe!


Our latest product:
green mouse!


(2)数据模型

典型的数据模型是树型结构,可以任意复杂和深层次,如下面的例子:

(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"
类似于目录的变量称为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

[@more@]

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/750220/viewspace-905564/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: Log4j配置过程
请登录后发表评论 登录
全部评论
  • 博文量
    83
  • 访问量
    198461