轉(zhuǎn)貼地址:http://blog.csdn.net/chenyun2000/archive/2004/11/17/184887.aspx
<#macro greet>
? <font size="+2">Hello Joe!</font>
</#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>
?
<#macro greet person color>
? <font size="+2" color="${color}">Hello ${person}!</font>
</#macro>?
<@greet person="Fred" color="black"/>?
<@greet color="black" person="Fred"/>
<@greet person="Fred" color="black" background="green"/>
<@greet person="Fred"/>
<#macro greet person color="black">
? <font size="+2" color="${color}">Hello ${person}!</font>
</#macro>?
<#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>
?
<#macro do_thrice>
? <#nested>
? <#nested>
? <#nested>
</#macro>
<@do_thrice>
? Anything.
</@do_thrice>?
? Anything.
? Anything.
? Anything.?
<@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: ? ? ?
<#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!
?
(2)在模板中定義變量
l???????? 在模板中定義的變量有三種類型:
????????? plain變量:可以在模板的任何地方訪問(wèn),包括使用include指令插入的模板,使用assign指令創(chuàng)建和替換
????????? 局部變量:在宏定義體中有效,使用local指令創(chuàng)建和替換
????????? 循環(huán)變量:只能存在于指令的嵌套內(nèi)容,由指令(如list)自動(dòng)創(chuàng)建;宏的參數(shù)是局部變量,而不是循環(huán)變量
l???????? 局部變量隱藏(而不是覆蓋)同名的plain變量;循環(huán)變量隱藏同名的局部變量和plain變量,下面是一個(gè)例子:
<#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???????? 內(nèi)部循環(huán)變量隱藏同名的外部循環(huán)變量,如:
<#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???????? 模板中的變量會(huì)隱藏(而不是覆蓋)數(shù)據(jù)模型中同名變量,如果需要訪問(wèn)數(shù)據(jù)模型中的同名變量,使用特殊變量global,下面的例子假設(shè)數(shù)據(jù)模型中的user的值是Big Joe:
<#assign user = "Joe Hider">
${user}????????? <#-- prints: Joe Hider -->
${.globals.user} <#-- prints: Big Joe -->?
(3)名字空間
l???????? 通常情況,只使用一個(gè)名字空間,稱為主名字空間
l???????? 為了創(chuàng)建可重用的宏、變換器或其它變量的集合(通常稱庫(kù)),必須使用多名字空間,其目的是防止同名沖突
l???????? 創(chuàng)建庫(kù)
????????? 下面是一個(gè)創(chuàng)建庫(kù)的例子(假設(shè)保存在lib/my_test.ftl中):
<#macro copyright date>
? <p>Copyright (C) ${date} Julia Smith. All rights reserved.
? <br>Email: ${mail}</p>
</#macro>?
<#assign mail = "jsmith@acme.com">?
????????? 使用import指令導(dǎo)入庫(kù)到模板中,F(xiàn)reemarker會(huì)為導(dǎo)入的庫(kù)創(chuàng)建新的名字空間,并可以通過(guò)import指令中指定的散列變量訪問(wèn)庫(kù)中的變量:
<#import "/lib/my_test.ftl" as my>
<#assign mail="fred@acme.com">
<@my.copyright date="1999-2002"/>
${my.mail}
${mail}?
輸出結(jié)果:
? <p>Copyright (C) 1999-2002 Julia Smith. All rights reserved.
? <br>Email: jsmith@acme.com</p>
jsmith@acme.com
fred@acme.com?
可以看到例子中使用的兩個(gè)同名變量并沒(méi)有沖突,因?yàn)樗鼈兾挥诓煌拿挚臻g
l???????? 可以使用assign指令在導(dǎo)入的名字空間中創(chuàng)建或替代變量,下面是一個(gè)例子:
<#import "/lib/my_test.ftl" as my>
${my.mail}
<#assign mail="jsmith@other.com" in my>
${my.mail}?
l???????? 輸出結(jié)果:
jsmith@acme.com
jsmith@other.com?
l???????? 數(shù)據(jù)模型中的變量任何地方都可見,也包括不同的名字空間,下面是修改的庫(kù):
<#macro copyright date>
? <p>Copyright (C) ${date} ${user}. All rights reserved.</p>
</#macro>
<#assign mail = "${user}@acme.com"> ?
l???????? 假設(shè)數(shù)據(jù)模型中的user變量的值是Fred,則下面的代碼:
<#import "/lib/my_test.ftl" as my>
<@my.copyright date="1999-2002"/>
${my.mail} ?
l???????? 輸出結(jié)果:
? <p>Copyright (C) 1999-2002 Fred. All rights reserved.</p>
Fred@acme.com ?
posted on 2006-04-19 18:06
OMG 閱讀(313)
評(píng)論(0) 編輯 收藏 所屬分類:
FreeMarker