FreeMarker設(shè)計指南(3)
2024-07-21 02:14:09
供稿:網(wǎng)友
freemarker設(shè)計指南(3)
3、模板
(1)整體結(jié)構(gòu)
l 模板使用ftl(freemarker模板語言)編寫,是下面各部分的一個組合:
ø 文本:直接輸出
ø interpolation:由${和},或#{和}來限定,計算值替代輸出
ø ftl標記:freemarker指令,和html標記類似,名字前加#予以區(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 注意事項:
ø ftl區(qū)分大小寫,所以list是正確的ftl指令,而list不是;${name}和${name}是不同的
ø interpolation只能在文本中使用
ø ftl標記不能位于另一個ftl標記內(nèi)部,例如:
<#if <#include 'foo'>='bar'>...</if>
ø 注釋可以位于ftl標記和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標記引用指令
l 有三種ftl標記,這和html標記是類似的:
ø 開始標記:<#directivename parameters>
ø 結(jié)束標記:</#directivename>
ø 空內(nèi)容指令標記:<#directivename parameters/>
l 有兩種類型的指令:預(yù)定義指令和用戶定義指令
l 用戶定義指令要使用@替換#,如<@mydirective>...</@mydirective>(會在后面講述)
l ftl標記不能夠交叉,而應(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標記中的空白字符,如下面的例子:
<#list[br]
animals as[br]
being[br]
>[br]
${being.name} for ${being.price} euros[br]
</#list >
l 但是,<、</和指令之間不允許有空白字符
(3)表達式
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)義序列:
轉(zhuǎ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"}
輸出的結(jié)果是:
${foo}
c:/foo/bar
ø 數(shù)字
n 直接輸入,不需要引號
n 精度數(shù)字使用“.”分隔,不能使用分組符號
n 目前版本不支持科學(xué)計數(shù)法,所以“1e3”是錯誤的
n 不能省略小數(shù)點前面的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 列表的項目是表達式,所以可以有下面的例子:
[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 鍵和值都是表達式,但是鍵必須是字符串
l 獲取變量
ø 頂層變量: ${variable},變量名只能是字母、數(shù)字、下劃線、$、@和#的組合,且不能以數(shù)字開頭
ø 從散列中獲取數(shù)據(jù)
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 使用點語法,變量名字有頂層變量一樣的限制,但方括號語法沒有該限制,因為名字是任意表達式的結(jié)果
ø 從序列獲得數(shù)據(jù):和散列的方括號語法語法一樣,只是方括號中的表達式值必須是數(shù)字;注意:第一個項目的索引是0
ø 序列片斷:使用[startindex..endindex]語法,從序列中獲得序列片斷(也是序列);startindex和endindex是結(jié)果為數(shù)字的表達式
ø 特殊變量:freemarker內(nèi)定義變量,使用.variablename語法訪問
l 字符串操作
ø interpolation(或連接操作)
n 可以使用${..}(或#{..})在文本部分插入表達式的值,例如:
${"hello ${user}!"}
${"${user}${user}${user}${user}"}
n 可以使用+操作符獲得同樣的結(jié)果
${"hello " + user + "!"}
${user + user + user + user}
n ${..}只能用于文本部分,下面的代碼是錯誤的:
<#if ${isbig}>wow!</#if>
<#if "${isbig}">wow!</#if>
應(yīng)該寫成:
<#if isbig>wow!</#if>
ø 子串
n 例子(假設(shè)user的值為“big joe”):
${user[0]}${user[4]}
${user[1..4]}
結(jié)果是(注意第一個字符的索引是0):
bj
ig j
l 序列操作
ø 連接操作:和字符串一樣,使用+,下面是一個例子:
<#list ["joe", "fred"] + ["julia", "kate"] as user>
- ${user}
</#list>
輸出結(jié)果是:
- joe
- fred
- julia
- kate
l 散列操作
ø 連接操作:和字符串一樣,使用+,如果具有相同的key,右邊的值替代左邊的值,例如:
<#assign ages = {"joe":23, "fred":25} + {"joe":30, "julia":18}>
- joe is ${ages.joe}
- fred is ${ages.fred}
- julia is ${ages.julia}
輸出結(jié)果是:
- joe is 30
- fred is 25
- julia is 18
l 算術(shù)運算
ø +、-、×、/、%,下面是一個例子:
${x * x - 100}
${x / 2}
${12 % 10}
輸出結(jié)果是(假設(shè)x為5):
-75
2.5
2
ø 操作符兩邊必須是數(shù)字,因此下面的代碼是錯誤的:
${3 * "5"} <#-- wrong! -->
ø 使用+操作符時,如果一邊是數(shù)字,一邊是字符串,就會自動將數(shù)字轉(zhuǎn)換為字符串,例如:
${3 + "5"}
輸出結(jié)果是:
35
ø 使用內(nèi)建的int(后面講述)獲得整數(shù)部分,例如:
${(x/2)?int}
${1.1?int}
${1.999?int}
${-1.1?int}
${-1.999?int}
輸出結(jié)果是(假設(shè)x為5):
2
1
1
-1
-1
l 比較操作符
ø 使用=(或==,完全相等)測試兩個值是否相等,使用!= 測試兩個值是否不相等
ø =和!=兩邊必須是相同類型的值,否則會產(chǎn)生錯誤,例如<#if 1 = "1">會引起錯誤
ø freemarker是精確比較,所以對"x"、"x "和"x"是不相等的
ø 對數(shù)字和日期可以使用<、<=、>和>=,但不能用于字符串
ø 由于freemarker會將>解釋成ftl標記的結(jié)束字符,所以對于>和>=可以使用括號來避免這種情況,例如<#if (x > y)>
ø 另一種替代的方法是,使用lt、lte、gt和gte來替代<、<=、>和>=
l 邏輯操作符
ø &&(and)、||(or)、!(not),只能用于布爾值,否則會產(chǎn)生錯誤
ø 例子:
<#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>
l 內(nèi)建函數(shù)
ø 內(nèi)建函數(shù)的用法類似訪問散列的子變量,只是使用“?”替代“.”,下面列出常用的一些函數(shù)
ø 字符串使用的:
n html:對字符串進行html編碼
n cap_first:使字符串第一個字母大寫
n lower_case:將字符串轉(zhuǎn)換成小寫
n upper_case:將字符串轉(zhuǎn)換成大寫
n trim:去掉字符串前后的空白字符
ø 序列使用的:
n size:獲得序列中元素的數(shù)目
ø 數(shù)字使用的:
n int:取得數(shù)字的整數(shù)部分(如-1.9?int的結(jié)果是-1)
ø 例子(假設(shè)test保存字符串"tom & jerry"):
${test?html}
${test?upper_case?html}
輸出結(jié)果是:
tom & jerry
tom & jerry
l 操作符優(yōu)先順序
操作符組
操作符
后綴
[subvarname] [substringrange] . (methodparams)
一元
+expr、-expr、!
內(nèi)建
?
乘法
*、 / 、%
加法
+、-
關(guān)系
<、>、<=、>=(lt、lte、gt、gte)
相等
==(=)、!=
邏輯and
&&
邏輯or
||
數(shù)字范圍
..
(4)interpolation
l interpolation有兩種類型:
ø 通用interpolation:${expr}
ø 數(shù)字interpolation:#{expr}或#{expr; format}
l 注意:interpolation只能用于文本部分
l 通用interpolation
ø 插入字符串值:直接輸出表達式結(jié)果
ø 插入數(shù)字值:根據(jù)缺省格式(由#setting指令設(shè)置)將表達式結(jié)果轉(zhuǎn)換成文本輸出;可以使用內(nèi)建函數(shù)string格式化單個interpolation,下面是一個例子:
<#setting number_format="currency"/>
<#assign answer=42/>
${answer}
${answer?string} <#-- the same as ${answer} -->
${answer?string.number}
${answer?string.currency}
${answer?string.percent}
輸出結(jié)果是:
$42.00
$42.00
42
$42.00
4,200%
ø 插入日期值:根據(jù)缺省格式(由#setting指令設(shè)置)將表達式結(jié)果轉(zhuǎn)換成文本輸出;可以使用內(nèi)建函數(shù)string格式化單個interpolation,下面是一個使用格式模式的例子:
${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')'")}
輸出的結(jié)果類似下面的格式:
2003-04-08 21:24:44 pacific daylight time
tue, apr 8, '03
tuesday, april 08, 2003, 09:24:44 pm (pdt)
ø 插入布爾值:根據(jù)缺省格式(由#setting指令設(shè)置)將表達式結(jié)果轉(zhuǎn)換成文本輸出;可以使用內(nèi)建函數(shù)string格式化單個interpolation,下面是一個例子:
<#assign foo=true/>
${foo?string("yes", "no")}
輸出結(jié)果是:
yes
l 數(shù)字interpolation的#{expr; format}形式可以用來格式化數(shù)字,format可以是:
ø mx:小數(shù)部分最小x位
ø mx:小數(shù)部分最大x位
ø 例子:
<#-- 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 -->