|
author:http://blog.csdn.net/chenyun2000/archive/2004/11/13/180780.aspx
3、模板 (1)整體結(jié)構(gòu) l 模板使用FTL(FreeMarker模板語言)編寫,是下面各部分的一個組合: Ø 文本:直接輸出 Ø Interpolation:由${和},或#{和}來限定,計(jì)算值替代輸出 Ø FTL標(biāo)記:FreeMarker指令,和HTML標(biāo)記類似,名字前加#予以區(qū)分,不會輸出 Ø 注釋:由<#--和-->限定,不會輸出 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 注意事項(xiàng): Ø FTL區(qū)分大小寫,所以list是正確的FTL指令,而List不是;${name}和${NAME}是不同的 Ø Interpolation只能在文本中使用 Ø FTL標(biāo)記不能位于另一個FTL標(biāo)記內(nèi)部,例如: <#if <#include ‘foo‘>=‘bar‘>...</if> Ø 注釋可以位于FTL標(biāo)記和Interpolation內(nèi)部,如下面的例子: <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標(biāo)記引用指令 l 有三種FTL標(biāo)記,這和HTML標(biāo)記是類似的: Ø 開始標(biāo)記:<#directivename parameters> Ø 結(jié)束標(biāo)記:</#directivename> Ø 空內(nèi)容指令標(biāo)記:<#directivename parameters/> l 有兩種類型的指令:預(yù)定義指令和用戶定義指令 l 用戶定義指令要使用@替換#,如<@mydirective>...</@mydirective>(會在后面講述) l FTL標(biāo)記不能夠交叉,而應(yīng)該正確的嵌套,如下面的代碼是錯誤的: <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不會使用模板輸出,而是產(chǎn)生一個錯誤消息 l FreeMarker會忽略FTL標(biāo)記中的空白字符,如下面的例子: <#list[BR]
animals as[BR]
being[BR]
>[BR] ${being.name} for ${being.price} Euros[BR]
</#list >
l 但是,<、</和指令之間不允許有空白字符 (3)表達(dá)式 l 直接指定值 Ø 字符串 n 使用單引號或雙引號限定 n 如果包含特殊字符需要轉(zhuǎn)義,如下面的例子: ${"It‘s \"quoted\" and this is a backslash: \\"}
${‘It\‘s "quoted" and this is a backslash: \\‘} 輸出結(jié)果是: It‘s "quoted" and this is a backslash: \
It‘s "quoted" and this is a backslash: \ n 下面是支持的轉(zhuǎn)義序列:
n 有一類特殊的字符串稱為raw字符串,被認(rèn)為是純文本,其中的\和{等不具有特殊含義,該類字符串在引號前面加r,下面是一個例子: ${r"${foo}"} ${r"C:\foo\bar"} 輸出的結(jié)果是: ${foo}
C:\foo\bar
Ø 數(shù)字 n 直接輸入,不需要引號 n 精度數(shù)字使用“.”分隔,不能使用分組符號 n 目前版本不支持科學(xué)計(jì)數(shù)法,所以“1E3”是錯誤的 n 不能省略小數(shù)點(diǎn)前面的0,所以“.5”是錯誤的 n 數(shù)字8、+8、08和8.00都是相同的 Ø 布爾值 n true和false,不使用引號 Ø 序列 n 由逗號分隔的子變量列表,由方括號限定,下面是一個例子: <#list ["winter", "spring", "summer", "autumn"] as x> ${x}
</#list>
輸出的結(jié)果是: winter
spring
summer
autumn
n 列表的項(xiàng)目是表達(dá)式,所以可以有下面的例子: [2 + 2, [1, 2, 3, 4], "whatnot"]
n 可以使用數(shù)字范圍定義數(shù)字序列,例如2..5等同于[2, 3, 4, 5],但是更有效率,注意數(shù)字范圍沒有方括號 n 可以定義反遞增的數(shù)字范圍,如5..2 Ø 散列(hash) n 由逗號分隔的鍵/值列表,由大括號限定,鍵和值之間用冒號分隔,下面是一個例子: {"name":"green mouse", "price":150}
n 鍵和值都是表達(dá)式,但是鍵必須是字符串 l 獲取變量 Ø 頂層變量: ${variable},變量名只能是字母、數(shù)字、下劃線、$、@和#的組合,且不能以數(shù)字開頭 Ø 從散列中獲取數(shù)據(jù) n 可以使用點(diǎn)語法或方括號語法,假設(shè)有下面的數(shù)據(jù)模型: (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 使用點(diǎn)語法,變量名字有頂層變量一樣的限制,但方括號語法沒有該限制,因?yàn)槊质侨我獗磉_(dá)式的結(jié)果 Ø 從序列獲得數(shù)據(jù):和散列的方括號語法語法一樣,只是方括號中的表達(dá)式值必須是數(shù)字;注意:第一個項(xiàng)目的索引是0 Ø 序列片斷:使用[startIndex..endIndex]語法,從序列中獲得序列片斷(也是序列);startIndex和endIndex是結(jié)果為數(shù)字的表達(dá)式 Ø 特殊變量:FreeMarker內(nèi)定義變量,使用.variablename語法訪問 ${"Hello ${user}!"}
${"${user}${user}${user}${user}"}
${"Hello " + user + "!"}
${user + user + user + user}
<#if ${isBig}>Wow!</#if>
<#if "${isBig}">Wow!</#if>
<#if isBig>Wow!</#if>
${user[0]}${user[4]}
${user[1..4]}
BJ
ig J
<#list ["Joe", "Fred"] + ["Julia", "Kate"] as user>
- ${user}
</#list>
- Joe
- Fred
- Julia
- Kate
<#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}
-75
2.5
2
${3 * "5"} <#-- WRONG! -->
${3 + "5"}
35
${(x/2)?int}
${1.1?int}
${1.999?int}
${-1.1?int}
${-1.999?int}
2
1
1
-1
-1
<#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
<#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")}
yes
<#-- 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 -->
|
|
|