一、FreeMarker 模版开发指南 第一章 入门

所有资料来自 南磊 翻译的官方文档,我弄简单了,适合自己以后拿出来翻看。

 章节内容如下:
  简介
  模板+数据模型=输出
  数据模型一览
  模板一览

一、模板  +  数据模型  =  输出

输出结果:

 1 <html> 
 2 <head> 
 3   <title>Welcome!</title> 
 4 </head> 
 5 <body> 
 6   <h1>Welcome Big Joe!</h1> 
 7   <p>Our latest product: 
 8   <a href="products/greenmouse.html">green mouse</a>! 
 9 </body> 
10 </html>

模版:

 1 <html> 
 2 <head> 
 3   <title>Welcome!</title> 
 4 </head> 
 5 <body> 
 6   <h1>Welcome ${user}!</h1> 
 7   <p>Our latest product: 
 8   <a href="${latestProduct.url}">${latestProduct.name}</a>! 
 9 </body> 
10 </html>

 数据模型

(root) 
  | 
  +- user = "Big Joe" 
  | 
  +- latestProduct 
      | 
      +- url = "products/greenmouse.html" 
      | 
      +- name = "green mouse" 

看上去是不是和servlet里面 把数据装到 request和session里差不多,然后JSP里用 表达式语言、脚本语言、或者标签来输出。

二、数据模型。

 sequences序列是一种非常重要的变量,它们和哈希表变量相似,但是它们不存储所包含变量的名称,而是按顺序存储子变量。这样,就可以使用数字索引来访问这些子变量。在这种数据模型中,animal和whatnot.fruits就是序列。

(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"

 hash哈希表。哈希表通过可查找的名称(例如:”animal”, ”mouse”, ”price”)来访问存储的其他变量(如子变量)。

 scalars标量,仅存储单值的变量(size,price,text和because)。

如果要在模板中使用子变量,那应该从根root 开始指定它的路径,每级之间用点来分隔。要访问price和mouse的话,应该从根开始,先是animals,然后是mouse,最后是price,所以应该这样写:animals.mouse.price。当放置${…}这种特定代码在表达式前后时,我们就告诉FreeMarker在那个位置上要来输出对应的文本。

(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"

 标量类型:字符串,数字,日期/时间,布尔值。

总结:
  数据模型可以被看做是树状结构。
  标量存储单一的值,这种类型的值可以是字符串,数字,日期/时间或者是布尔值。
  哈希表是存储变量和与其相关且有唯一标识名称变量的容器。
  序列是存储有序变量的容器。存储的变量可以通过数字索引来检索,索引通常从零开始。

三、模版

a、简介

  ${…}:FreeMarker 将会输出真实的值来替换花括号内的表达式,这样被称为interpolations 插值。

  FTL tags标签(FreeMarker 模板的语言标签):FTL标签和HTML标签有一点相似,但是它们是FreeMarker 的指令而且是不会直接输出出来的东西。这些标签的使用一般以符号#开头。(用户自定义的FTL标签使用@符号来代替#,以后讨论)

  Comments注释:FreeMarker的注释和HTML的注释相似,但是它用<#--和-->来分隔。任何介于这两个分隔符(包含分隔符本身)之间内容会被FreeMarker 忽略,就不会输出出来了。

  其他任何不是FT L 标签,插值或注释的内容将被视为静态文本,这些东西就不会被FreeMarker所解析,会被按照原样输出出来。

  directives 指令就是所指的FTL 标签。这些指令在HTML的标签(如<table>和</table>)元素(如table元素)中的关系是相同的。(如果现在你还不能区分它们,把“FTL标签”和“指令”看做是同义词即可。)

b、指令示例

if 指令 <#if condition> [ content ] </#if>

使用if 指令可以有条件地跳过模板的一部分,这和程序语言中if 是相似的。假设在第一个示例中,你只想向你的老板Big Joe(而不是其他人)问好,就可以这样做:

 1 <html> 
 2 <head> 
 3   <title>Welcome!</title> 
 4 </head> 
 5 <body> 
 6   <h1> 
 7     Welcome ${user}<#if user == "Big Joe">, our beloved 
 8 leader</#if>! 
 9   </h1> 
10   <p>Our latest product: 
11   <a href="${latestProduct.url}">${latestProduct.name}</a>! 
12 </body> 
13 </html>
<#if animals.python.price == 0> 
  Pythons are free today! 
</#if>
<#if animals.python.price < animals.elephant.price> 
  Pythons are cheaper than elephants today. 
<#else> 
  Pythons are not cheaper than elephants today. 
</#if>

list 指令  <#list sequence as loopVariable>repeatThis</#list>

<p>And BTW we have these fruits: 
<ul> 
<#list whatnot.fruits as fruit> 
 <li>${fruit} </li>
</#list> 
<ul> 

include 指令

使用include指令,我们可以插入其他文件的内容到当前的模板中。

copyright_footer.html

1 <hr> 
2 <i> 
3 Copyright (c) 2000 <a href="http://www.acmee.com">Acmee 
4 Inc</a>, 
5 <br> 
6 All Rights Reserved. 
7 </i>

any page which needs  copyright_footer.html

 1 <html> 
 2 <head> 
 3   <title>Test page</title> 
 4 </head> 
 5 <body> 
 6   <h1>Test page</h1> 
 7   <p>Blah blah... 
 8 <#include "/copyright_footer.html"> 
 9 </body> 
10 </html> 

output

 1 <html> 
 2 <head> 
 3   <title>Test page</title> 
 4 </head> 
 5 <body> 
 6   <h1>Test page</h1> 
 7   <p>Blah blah... 
 8 <hr> 
 9 <i> 
10 Copyright (c) 2000 <a href="http://www.acmee.com">Acmee 
11 Inc</a>, 
12 <br> 
13 All Rights Reserved. 
14 </i> 
15 </body> 
16 </html>

联合使用指令

在页面也可以多次使用指令,而且指令间可以相互嵌套,正如在HTML元素中嵌套使用标签一样。下面的代码会遍历动物,用大号字体来打印大型动物的名字。

 1 <p>We have these animals: 
 2 <table border=1> 
 3   <tr><th>Name<th>Price 
 4   <#list animals as being> 
 5   <tr> 
 6     <td> 
 7       <#if being.size == "large"><font size="+1"></#if> 
 8       ${being.name} 
 9       <#if being.size == "large"></font></#if> 
10     <td>${being.price} Euros 
11   </#list> 
12 </table> 

处理不存在的变量

当user从数据模型中丢失时,模板将会将user’S 的值表现为字符串”Anonymous”。(若 user并没有丢失,那么模板就表现出”Anonymous”不存在一样):

<h1>Welcome ${user!"Anonymous"}!</h1> 

当然也可以通过放置??在变量名后面来询问FreeMarker一个变量是否存在。将它和if指令合并,那么如果user变量不存在的话将会忽略整个问候代码段:

<#if user??><h1>Welcome ${user}!</h1></#if>

 关于多级访问的变量,比如animals.python.price,书写代码:animals.python.price!0,仅当animals.python存在而仅仅最后一个子变量price可能不存在(这种情况下我们假设价格是0)。如果animals或者python不存在,那么模板处理将会以“未定义的变量”错误停止。为了防止这种情况的发生,可以这样来书写代码(animals.python.price)!0。这种情况下当animals 或python不存在时表达式的结果仍然是0。对于??也是同样用来的处理这种逻辑的:animals.python.price??对比(animals.python.price)??

原文地址:https://www.cnblogs.com/xunol/p/3259112.html