FreeMarker设计指南(一)

来源:互联网 发布:个人淘宝开店流程 编辑:程序博客网 时间:2024/05/02 13:56
 
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         至于userlatestProduct.urllatestProduct.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模板中可以包括下面三种特定部分:
Ø         ${…}:称为interpolationsFreeMarker会在输出时用实际值进行替代
Ø         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> 
 
 
原创粉丝点击