FreeMarker設計指南(4) (完)
2024-07-21 02:14:10
供稿:網友
本文來源于網頁設計愛好者web開發社區http://www.html.org.cn收集整理,歡迎訪問。freemarker設計指南(4) (完)
4、雜項
(1)用戶定義指令
l 宏和變換器變量是兩種不同類型的用戶定義指令,它們之間的區別是宏是在模板中使用macro指令定義,而變換器是在模板外由程序定義,這里只介紹宏
l 基本用法
ø 宏是和某個變量關聯的模板片斷,以便在模板中通過用戶定義指令使用該變量,下面是一個例子:
<#macro greet>
<font size="+2">hello joe!</font>
</#macro>
ø 作為用戶定義指令使用宏變量時,使用@替代ftl標記中的#
<@greet></@greet>
ø 如果沒有體內容,也可以使用:
<@greet/>
l 參數
ø 在macro指令中可以在宏變量之后定義參數,如:
<#macro greet person>
<font size="+2">hello ${person}!</font>
</#macro>
ø 可以這樣使用這個宏變量:
<@greet person="fred"/> and <@greet person="batman"/>
輸出結果是:
<font size="+2">hello fred!</font>
and <font size="+2">hello batman!</font>
ø 宏的參數是ftl表達式,所以下面的代碼具有不同的意思:
<@greet person=fred/>
ø 這意味著將fred變量的值傳給person參數,該值不僅是字符串,還可以是其它類型,甚至是復雜的表達式
ø 宏可以有多參數,下面是一個例子:
<#macro greet person color>
<font size="+2" color="${color}">hello ${person}!</font>
</#macro>
ø 可以這樣使用該宏變量:
<@greet person="fred" color="black"/>
ø 其中參數的次序是無關的,因此下面是等價的:
<@greet color="black" person="fred"/>
ø 只能使用在macro指令中定義的參數,并且對所有參數賦值,所以下面的代碼是錯誤的:
<@greet person="fred" color="black" background="green"/>
<@greet person="fred"/>
ø 可以在定義參數時指定缺省值,如:
<#macro greet person color="black">
<font size="+2" color="${color}">hello ${person}!</font>
</#macro>
ø 這樣<@greet person="fred"/>就正確了
ø 宏的參數是局部變量,只能在宏定義中有效
l 嵌套內容
ø 用戶定義指令可以有嵌套內容,使用<#nested>指令執行指令開始和結束標記之間的模板片斷
ø 例子:
<#macro border>
<table border=4 cellspacing=0 cellpadding=4><tr><td>
<#nested>
</tr></td></table>
</#macro>
這樣使用該宏變量:
<@border>the bordered text</@border>
輸出結果:
<table border=4 cellspacing=0 cellpadding=4><tr><td>
the bordered text
</tr></td></table>
ø <#nested>指令可以被多次調用,例如:
<#macro do_thrice>
<#nested>
<#nested>
<#nested>
</#macro>
<@do_thrice>
anything.
</@do_thrice>
輸出結果:
anything.
anything.
anything.
ø 嵌套內容可以是有效的ftl,下面是一個有些復雜的例子:
<@border>
<ul>
<@do_thrice>
<li><@greet person="joe"/>
</@do_thrice>
</ul>
</@border>
輸出結果:
<table border=4 cellspacing=0 cellpadding=4><tr><td>
<ul>
<li><font size="+2">hello joe!</font>
<li><font size="+2">hello joe!</font>
<li><font size="+2">hello joe!</font>
</ul>
</tr></td></table>
ø 宏定義中的局部變量對嵌套內容是不可見的,例如:
<#macro repeat count>
<#local y = "test">
<#list 1..count as x>
${y} ${count}/${x}: <#nested>
</#list>
</#macro>
<@repeat count=3>${y?default("?")} ${x?default("?")} ${count?default("?")}</@repeat>
輸出結果:
test 3/1: ? ? ?
test 3/2: ? ? ?
test 3/3: ? ? ?
ø
l 在宏定義中使用循環變量
ø 用戶定義指令可以有循環變量,通常用于重復嵌套內容,基本用法是:作為nested指令的參數傳遞循環變量的實際值,而在調用用戶定義指令時,在<@…>開始標記的參數后面指定循環變量的名字
ø 例子:
<#macro repeat count>
<#list 1..count as x>
<#nested x, x/2, x==count>
</#list>
</#macro>
<@repeat count=4 ; c, halfc, last>
${c}. ${halfc}<#if last> last!</#if>
</@repeat>
輸出結果:
1. 0.5
2. 1
3. 1.5
4. 2 last!
ø 指定的循環變量的數目和用戶定義指令開始標記指定的不同不會有問題
n 調用時少指定循環變量,則多指定的值不可見
n 調用時多指定循環變量,多余的循環變量不會被創建
(2)在模板中定義變量
l 在模板中定義的變量有三種類型:
ø plain變量:可以在模板的任何地方訪問,包括使用include指令插入的模板,使用assign指令創建和替換
ø 局部變量:在宏定義體中有效,使用local指令創建和替換
ø 循環變量:只能存在于指令的嵌套內容,由指令(如list)自動創建;宏的參數是局部變量,而不是循環變量
l 局部變量隱藏(而不是覆蓋)同名的plain變量;循環變量隱藏同名的局部變量和plain變量,下面是一個例子:
<#assign x = "plain">
1. ${x} <#-- we see the plain var. here -->
<@test/>
6. ${x} <#-- the value of plain var. was not changed -->
<#list ["loop"] as x>
7. ${x} <#-- now the loop var. hides the plain var. -->
<#assign x = "plain2"> <#-- replace the plain var, hiding does not mater here -->
8. ${x} <#-- it still hides the plain var. -->
</#list>
9. ${x} <#-- the new value of plain var. -->
<#macro test>
2. ${x} <#-- we still see the plain var. here -->
<#local x = "local">
3. ${x} <#-- now the local var. hides it -->
<#list ["loop"] as x>
4. ${x} <#-- now the loop var. hides the local var. -->
</#list>
5. ${x} <#-- now we see the local var. again -->
</#macro>
輸出結果:
1. plain
2. plain
3. local
4. loop
5. local
6. plain
7. loop
8. loop
9. plain2
l 內部循環變量隱藏同名的外部循環變量,如:
<#list ["loop 1"] as x>
${x}
<#list ["loop 2"] as x>
${x}
<#list ["loop 3"] as x>
${x}
</#list>
${x}
</#list>
${x}
</#list>
輸出結果:
loop 1
loop 2
loop 3
loop 2
loop 1
l 模板中的變量會隱藏(而不是覆蓋)數據模型中同名變量,如果需要訪問數據模型中的同名變量,使用特殊變量global,下面的例子假設數據模型中的user的值是big joe:
<#assign user = "joe hider">
${user} <#-- prints: joe hider -->
${.globals.user} <#-- prints: big joe -->
(3)名字空間
l 通常情況,只使用一個名字空間,稱為主名字空間
l 為了創建可重用的宏、變換器或其它變量的集合(通常稱庫),必須使用多名字空間,其目的是防止同名沖突
l 創建庫
ø 下面是一個創建庫的例子(假設保存在lib/my_test.ftl中):
<#macro copyright date>
<p>copyright (c) ${date} julia smith. all rights reserved.
<br>email: ${mail}</p>
</#macro>
<#assign mail = "[email protected]">
ø 使用import指令導入庫到模板中,freemarker會為導入的庫創建新的名字空間,并可以通過import指令中指定的散列變量訪問庫中的變量:
<#import "/lib/my_test.ftl" as my>
<#assign mail="[email protected]">
<@my.copyright date="1999-2002"/>
${my.mail}
${mail}
輸出結果:
<p>copyright (c) 1999-2002 julia smith. all rights reserved.
<br>email: [email protected]</p>
[email protected]
[email protected]
可以看到例子中使用的兩個同名變量并沒有沖突,因為它們位于不同的名字空間
l 可以使用assign指令在導入的名字空間中創建或替代變量,下面是一個例子:
<#import "/lib/my_test.ftl" as my>
${my.mail}
<#assign mail="[email protected]" in my>
${my.mail}
l 輸出結果:
[email protected]
[email protected]
l 數據模型中的變量任何地方都可見,也包括不同的名字空間,下面是修改的庫:
<#macro copyright date>
<p>copyright (c) ${date} ${user}. all rights reserved.</p>
</#macro>
<#assign mail = "${user}@acme.com">
l 假設數據模型中的user變量的值是fred,則下面的代碼:
<#import "/lib/my_test.ftl" as my>
<@my.copyright date="1999-2002"/>
${my.mail}
l 輸出結果:
<p>copyright (c) 1999-2002 fred. all rights reserved.</p>
[email protected]
[點擊此處收藏本文] 發表于 2004年11月17日 10:14 pm -->flylyke 發表于2004-11-19 12:48 am 老兄,翻譯的好yny 發表于2004-12-22 12:15 pm 老兄,翻譯的好 steven 發表于2005-02-24 8:40 pm 這幾天我也在看freemaker,我想請問一個問題, freemarker.template 下的 addmacro() 和addimport() 兩個方法是干什么的,怎么用,我看了半天,謝謝了