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 下面是支持的轉義序列:
n 有一類特殊的字符串稱爲raw字符串,被認爲是純文本,其中的/和{等不具有特殊含義,該類字符串在引號前面加r,下面是一個例子: ${r"${foo}"} ${r"C:/foo/bar"} 輸出的結果是: ${foo} 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> 輸出的結果是: winter spring summer autumn 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> <#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 --> |
FreeMarker設計指南(3)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.