您的位置:首页 > 其它

FreeMarker设计指南(1)

2013-08-28 19:10 393 查看
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>


阅读(422) | 评论(0) | 转发(0) |

0
上一篇:总结常见电脑技巧大全

下一篇:FreeMarker设计指南(2)

相关热门文章

类的设计原则:单一职责SPR、...

常青iTop运维管理门户实施讲座...

常青IT资产管理套件公测产管理...

 某集团IT资产管理典型案例分...

常青OwnCloud云存储网盘讲座(...

JDK1.6官方下载_JDK6官方下载_...

MyEclipse6.5下载及注册码...

Eclipse+MyEclipse的配置

Eclipse 插件安装、升级和卸载...

最新版SWT Designer 6.0 安装,...

php集成环境和自己配置的区别...

flash播放控件

查看nginx某一时段的日志...

ftp服务器日志分析

mpi 目标机器积极拒绝,下面错...

给主人留下些什么吧!~~

评论热议
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: