轉貼地址:http://blog.csdn.net/chenyun2000/archive/2004/11/13/180780.aspx
3、模板
(1)整體結構
l???????? 模板使用FTL(FreeMarker模板語言)編寫,是下面各部分的一個組合:
????????? 文本:直接輸出
????????? Interpolation:由${和},或#{和}來限定,計算值替代輸出
????????? FTL標記:FreeMarker指令,和HTML標記類似,名字前加#予以區分,不會輸出
????????? 注釋:由<#--和-->限定,不會輸出
l???????? 下面是以一個具體模板例子:
<html>[BR]
<head>[BR]
??<title>Welcome!</title>[BR]
</head>[BR]
<body>[BR]
??<#--?Greet?the?user?with?his/her?name?-->[BR]
??<h1>Welcome?${user}!</h1>[BR]
??<p>We?have?these?animals:[BR]
??<ul>[BR]
??<#list?animals?as?being>[BR]
????<li>${being.name}?for?${being.price} Euros[BR]
??</#list>[BR]
??</ul>[BR]
</body>[BR]
</html>?
l???????? [BR]是用于換行的特殊字符序列
l???????? 注意事項:
????????? FTL區分大小寫,所以list是正確的FTL指令,而List不是;${name}和${NAME}是不同的
????????? Interpolation只能在文本中使用
????????? FTL標記不能位于另一個FTL標記內部,例如:
<#if <#include 'foo'>='bar'>...</if>
????????? 注釋可以位于FTL標記和Interpolation內部,如下面的例子:
<h1>Welcome?${user <#-- The name of user -->}!</h1>[BR]
<p>We?have?these?animals:[BR]
<ul>[BR]
<#list?<#-- some comment... --> animals as?<#-- again... --> being>[BR]
...?
????????? 多余的空白字符會在模板輸出時移除
(2)指令
l???????? 在FreeMarker中,使用FTL標記引用指令
l???????? 有三種FTL標記,這和HTML標記是類似的:
????????? 開始標記:<#directivename parameters>
????????? 結束標記:</#directivename>
????????? 空內容指令標記:<#directivename parameters/>
l???????? 有兩種類型的指令:預定義指令和用戶定義指令
l???????? 用戶定義指令要使用@替換#,如<@mydirective>...</@mydirective>(會在后面講述)
l???????? FTL標記不能夠交叉,而應該正確的嵌套,如下面的代碼是錯誤的:
<ul>
<#list animals as being>
? <li>${being.name} for ${being.price} Euros
? <#if use = "Big Joe">
???? (except for you)
</#list>
</#if><#-- WRONG! -->
</ul>?
l???????? 如果使用不存在的指令,FreeMarker不會使用模板輸出,而是產生一個錯誤消息
l???????? FreeMarker會忽略FTL標記中的空白字符,如下面的例子:
<#list[BR]
??animals???????as[BR]
?????being[BR]
>[BR]
${being.name}?for?${being.price}?Euros[BR]
</#list????>?
l???????? 但是,<、</和指令之間不允許有空白字符
(3)表達式
l???????? 直接指定值
????????? 字符串
n???????? 使用單引號或雙引號限定
n???????? 如果包含特殊字符需要轉義,如下面的例子:
${"It's \"quoted\" and
this is a backslash: \\"}
?
${'It\'s "quoted" and
this is a backslash: \\'}?
輸出結果是:
It's "quoted" and
this is a backslash: \
?
It's "quoted" and
this is a backslash: \?
n???????? 下面是支持的轉義序列:
轉義序列 | 含義 |
\" | 雙引號(u0022) |
\' | 單引號(u0027) |
\\ | 反斜杠(u005C) |
\n | 換行(u000A) |
\r | Return (u000D) |
\t | Tab (u0009) |
\b | Backspace (u0008) |
\f | Form feed (u000C) |
\l | < |
\g | > |
\a | & |
\{ | { |
\xCode | 4位16進制Unicode代碼 |
n???????? 有一類特殊的字符串稱為raw字符串,被認為是純文本,其中的\和{等不具有特殊含義,該類字符串在引號前面加r,下面是一個例子:
${r"${foo}"}
${r"C:\foo\bar"}?
輸出的結果是:
????????? 數字
n???????? 直接輸入,不需要引號
n???????? 精度數字使用“.”分隔,不能使用分組符號
n???????? 目前版本不支持科學計數法,所以“1E3”是錯誤的
n???????? 不能省略小數點前面的0,所以“.5”是錯誤的
n???????? 數字8、+8、08和8.00都是相同的
????????? 布爾值
n???????? true和false,不使用引號
????????? 序列
n???????? 由逗號分隔的子變量列表,由方括號限定,下面是一個例子:
<#list ["winter", "spring", "summer", "autumn"] as x>
${x}
</#list>?
輸出的結果是:
n???????? 列表的項目是表達式,所以可以有下面的例子:
[2 + 2, [1, 2, 3, 4], "whatnot"]
n???????? 可以使用數字范圍定義數字序列,例如2..5等同于[2, 3, 4, 5],但是更有效率,注意數字范圍沒有方括號
n???????? 可以定義反遞增的數字范圍,如5..2
????????? 散列(hash)
n???????? 由逗號分隔的鍵/值列表,由大括號限定,鍵和值之間用冒號分隔,下面是一個例子:
{"name":"green?mouse", "price":150}
n???????? 鍵和值都是表達式,但是鍵必須是字符串
l???????? 獲取變量
????????? 頂層變量: ${variable},變量名只能是字母、數字、下劃線、$、@和#的組合,且不能以數字開頭
????????? 從散列中獲取數據
n???????? 可以使用點語法或方括號語法,假設有下面的數據模型:
(root)
|
+- book
|?? |
|?? +- title = "Breeding green mouses"
|?? |
|?? +- author
|?????? |
|?????? +- name = "Julia Smith"
|?????? |
|?????? +- info = "Biologist, 1923-1985, Canada"
|
+- test = "title"?
下面都是等價的:
book.author.name
book["author"].name
book.author.["name"]
book["author"]["name"]
n???????? 使用點語法,變量名字有頂層變量一樣的限制,但方括號語法沒有該限制,因為名字是任意表達式的結果
????????? 從序列獲得數據:和散列的方括號語法語法一樣,只是方括號中的表達式值必須是數字;注意:第一個項目的索引是0
????????? 序列片斷:使用[startIndex..endIndex]語法,從序列中獲得序列片斷(也是序列);startIndex和endIndex是結果為數字的表達式
????????? 特殊變量:FreeMarker內定義變量,使用.variablename語法訪問
${"Hello ${user}!"}
${"${user}${user}${user}${user}"}?
${"Hello " + user + "!"}
${user + user + user + user}
<#if ${isBig}>Wow!</#if>
<#if "${isBig}">Wow!</#if>
${user[0]}${user[4]}
${user[1..4]}
<#list ["Joe", "Fred"] + ["Julia", "Kate"] as user>
- ${user}
</#list>
<#assign ages = {"Joe":23, "Fred":25} + {"Joe":30, "Julia":18}>
- Joe is ${ages.Joe}
- Fred is ${ages.Fred}
- Julia is ${ages.Julia}?
- Joe is 30
- Fred is 25
- Julia is 18?
${x * x - 100}
${x / 2}
${12 % 10}
${3 * "5"} <#-- WRONG! -->?
${(x/2)?int}
${1.1?int}
${1.999?int}
${-1.1?int}
${-1.999?int}
<#if x < 12 && color = "green">
? We have less than 12 things, and they are green.
</#if>
<#if !hot> <#-- here hot must be a boolean -->
? It's not hot.
</#if>?
${test?html}
${test?upper_case?html}
Tom & Jerry
TOM & JERRY?
操作符組 | 操作符 |
后綴 | [subvarName] [subStringRange]. (methodParams) |
一元 | +expr、-expr、! |
內建 | ? |
乘法 | *、 / 、% |
加法 | +、- |
關系 | <、>、<=、>=(lt、lte、gt、gte) |
相等 | ==(=)、!= |
邏輯and | && |
邏輯or | || |
數字范圍 | .. |
<#setting number_format="currency"/>
<#assign answer=42/>
${answer}
${answer?string}? <#-- the same as ${answer} -->
${answer?string.number}
${answer?string.currency}
${answer?string.percent}?
$42.00
$42.00
42
$42.00
4,200%
${lastUpdated?string("yyyy-MM-dd HH:mm:ss zzzz")}
${lastUpdated?string("EEE, MMM d, ''yy")}
${lastUpdated?string("EEEE, MMMM dd, yyyy, hh:mm:ss a '('zzz')'")}?
2003-04-08 21:24:44 Pacific Daylight Time
Tue, Apr 8, '03
Tuesday, April 08, 2003, 09:24:44 PM (PDT)
<#assign foo=true/>
${foo?string("yes", "no")}
?????????? <#-- If the language is US English the output is: -->
<#assign x=2.582/>
<#assign y=4/>
#{x; M2}?? <#-- 2.58 -->
#{y; M2}?? <#-- 4??? -->
#{x; m1}?? <#-- 2.6 -->
#{y; m1}?? <#-- 4.0 -->
#{x; m1M2} <#-- 2.58 -->
#{y; m1M2} <#-- 4.0? -->?
posted on 2006-04-19 18:05
OMG 閱讀(304)
評論(0) 編輯 收藏 所屬分類:
FreeMarker