Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1080708
  • 博文数量: 282
  • 博客积分: 10865
  • 博客等级: 上将
  • 技术积分: 2480
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-12 12:35
文章存档

2017年(1)

2016年(3)

2015年(10)

2014年(12)

2013年(5)

2012年(10)

2011年(29)

2010年(3)

2008年(13)

2007年(92)

2006年(104)

我的朋友

分类: Java

2007-04-22 18:34:07

1、快速入门

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

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

l         他们是分工劳动的:设计者专注于表示——创建HTML文件、图片、Web页面的其它可视化方面;程序员创建系统,生成设计页面要显示的数据

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

l         在这里,你可以在HTML(或其它要输出的文本)中加入一些特定指令,FreeMarker会在输出页面给最终用户时,用适当的数据替代这些代码

l         下面是一个例子:

  Welcome!
  

Welcome ${user}!

  

Our latest product:

  ${latestProduct.url}">${latestProduct.name}!

  

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可以看作是目录,而userurlname看作是文件,urlname文件位于latestProduct目录中(这只是一个比喻,实际并不存在)

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

  Welcome!
  

Welcome Big Joe!

  

Our latest product:

  products/greenmouse.html">green mouse!
  

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.
  

Ø         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
  
 

阅读(1459) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~