<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-18  評(píng)論-8  文章-0  trackbacks-0

    用戶定義指令-使用@符合來調(diào)用
    有兩種不同的類型:Macro(宏)和transform(傳遞器),Macro是在模板中使用macro指令定義,而transform是在模板外由程序定義(基本上都是基于Java的),這里通過Macro來介紹自定義指令。
    例一:
    <#macro greet>
      <font size="+2">Hello Joe!</font>
    </#macro> 
    使用:<@greet></@greet> 或 <@greet/>
    結(jié)果:<font size="+2">Hello Joe!</font>

    參數(shù)-在macro指令中可以在宏變量之后定義參數(shù)
    例二:
    <#macro greet person>
      <font size="+2">Hello ${person}!</font>
    </#macro>
    使用:<@greet person="Fred"/> and <@greet person="Batman"/>
    結(jié)果: <font size="+2">Hello Fred!</font> and <font size="+2">Hello Batman!</font>

    macro可以有多個(gè)參數(shù),參數(shù)的次序是無關(guān)的,在macro指令中只能使用定義的參數(shù),并且必須對(duì)所有參數(shù)賦值,可以在定義參數(shù)時(shí)指定缺省值:

    <#macro greet person color="black">
      
    <font size="+2" color="${color}">Hello ${person}!</font>
    </#macro> 


    在自定義指令嵌套內(nèi)容:模板片斷中使用<#nested>指令

    <#macro border>
      
    <table border=4 cellspacing=0 cellpadding=4><tr><td>
        
    <#nested>
      
    </tr></td></table>
    </#macro>

    使用:<@border>The bordered text</@border>
    結(jié)果:

    <table border=4 cellspacing=0 cellpadding=4>
    <tr><td>The bordered text
    </tr></td></table>

    <#nested>指令可以被多次調(diào)用:

    <#macro do_thrice>
      
    <#nested>
      
    <#nested>
      
    <#nested>
    </#macro>

    使用:
    <@do_thrice>Anything.</@do_thrice
    結(jié)果:
    Anything.
    Anything.
    Anything.

    注意:嵌套內(nèi)容是無法訪問到macro中的局部變量的。
    例如:

    <#macro repeat count>
      
    <#local = "test">
      
    <#list 1..count as x>
        ${y} ${count}/${x}: 
    <#nested>
      
    </#list>
    </#macro>
    <@repeat count=3>${y?default("?")} ${x?default("?")} ${count?default("?")}</@repeat>

    結(jié)果:
    test 3/1: ? ? ?
    test 3/2: ? ? ?
    test 3/3: ? ? ?


    下面是一個(gè)嵌套使用自定義指令的例子:

    <@border>
      
    <ul>
      
    <@do_thrice>
        
    <li><@greet person="Joe"/>
      
    </@do_thrice>
      
    </ul>
    </@border> 

    結(jié)果:

    <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中使用循環(huán)變量-作為nested指令的參數(shù)傳遞循環(huán)變量的實(shí)際值,而在調(diào)用用戶定義指令時(shí),在<@…>開始標(biāo)記的參數(shù)后面指定循環(huán)變量的名字:

    <#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

     結(jié)果:
    1. 0.5
    2. 1
    3. 1.5
    4. 2 Last!

    注意:循環(huán)變量和用戶定義指令開始標(biāo)記指定的數(shù)目可以不同,調(diào)用時(shí)少指定循環(huán)變量,則多指定的值不可見,調(diào)用時(shí)多指定循環(huán)變量,多余的循環(huán)變量不會(huì)被創(chuàng)建。

    模板中的變量,有三種類型:
    1.) plain(全局)變量:可以在模板的任何地方訪問,包括使用include指令插入的模板,使用assign指令創(chuàng)建和替換
    2.) 局部變量:在macro中有效,使用local指令創(chuàng)建和替換
    3.) 循環(huán)變量:只能存在于指令的嵌套內(nèi)容,由指令(如list)自動(dòng)創(chuàng)建;宏的參數(shù)是局部變量,而不是循環(huán)變量

    用assign指令創(chuàng)建和替換的例子:

    <#assign = 1>  <#-- create variable x -->
    ${x}
    <#assign = x + 3> <#-- replace variable x -->
    ${x}  

    結(jié)果:
    1
    4

    局部變量隱藏(而不是覆蓋)同名的plain變量;循環(huán)變量隱藏同名的局部變量和plain變量,下面是一個(gè)例子:

    <#assign = "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 = "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 = "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>  

    結(jié)果:
    1. plain
      2. plain
      3. local
        4. loop
      5. local
    6. plain
        7. loop
        8. loop
    9. plain2

    內(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>

    結(jié)果:
    loop 1
        loop 2
          loop 3
        loop 2
      loop 1

    模板中的變量會(huì)隱藏(而不是覆蓋)數(shù)據(jù)模型中同名變量,如果需要訪問數(shù)據(jù)模型中的同名變量,使用特殊變量global,下面的例子假設(shè)數(shù)據(jù)模型中的user的值是Big Joe:

    <#assign user = "Joe Hider">
    ${user}          
    <#-- prints: Joe Hider -->
    ${.globals.user} 
    <#-- prints: Big Joe -->  

    命名(namespaces)空間-通常情況,只使用一個(gè)命名空間,稱為主命名空間(main namespace),但你是不會(huì)意識(shí)到這些的;為了創(chuàng)建可重用的macro、transforms或其它變量的集合(通常稱庫),必須使用多命名空間,為了防止同名沖突。

    首先創(chuàng)建一個(gè)庫(假設(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)入庫到模板中,F(xiàn)reemarker會(huì)為導(dǎo)入的庫創(chuàng)建新的命名空間,并可以通過import指令中指定的hash(散列)變量訪問庫中的變量:

    <#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è)同名變量并沒有沖突,因?yàn)樗鼈兾挥诓煌拿臻g

    可以使用assign指令在導(dǎo)入的命名空間中創(chuàng)建或替代變量:

    <#import "/lib/my_test.ftl" as my>
    ${my.mail}
    <#assign mail="jsmith@other.com" in my>
    ${my.mail}  

    結(jié)果:
    jsmith@acme.com
    jsmith@other.com 

    數(shù)據(jù)模型中的變量任何地方都可見,也包括不同的命名空間,下面修改了剛才創(chuàng)建的庫:

    <#macro copyright date>
      
    <p>Copyright (C) ${date} ${user}. All rights reserved.</p>
    </#macro>
    <#assign mail = "${user}@acme.com">   

    假設(shè)數(shù)據(jù)模型中的user變量的值是Fred:

    <#import "/lib/my_test.ftl" as my>
    <@my.copyright date="1999-2002"/>
    ${my.mail}

     結(jié)果:
     <p>Copyright (C) 1999-2002 Fred. All rights reserved.</p>
    Fred@acme.com 


    參考:  
                
    FreeMarker  in sourceforge.net

    posted on 2005-03-11 11:08 阿姆斯壯 閱讀(2365) 評(píng)論(0)  編輯  收藏 所屬分類: 工具箱

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲成人免费网站| 免费国产草莓视频在线观看黄| 亚洲日韩在线观看免费视频| 在线观看亚洲免费视频| 国产午夜亚洲精品| 无码人妻一区二区三区免费手机| 亚洲性69影院在线观看| 97热久久免费频精品99| 77777亚洲午夜久久多喷| 四虎在线视频免费观看| 亚洲av无码无线在线观看| 国产小视频免费观看| 一级毛片免费一级直接观看| 夜夜春亚洲嫩草影院| 久久九九全国免费| 噜噜噜亚洲色成人网站∨| 国产精品色拉拉免费看| 亚洲精品国产第一综合99久久| 国产免费观看黄AV片| 久久久久免费视频| 精品亚洲成AV人在线观看| 国产精品视频永久免费播放| 亚洲av无码片vr一区二区三区| 亚洲毛片av日韩av无码| 久久国产精品国产自线拍免费| 亚洲1区1区3区4区产品乱码芒果| 永久免费AV无码网站在线观看| jizz18免费视频| 亚洲自偷精品视频自拍| 免费的一级黄色片| 二个人看的www免费视频| 亚洲精品视频观看| 国产婷婷高清在线观看免费| 花蝴蝶免费视频在线观看高清版| 久久亚洲国产精品成人AV秋霞| 永久中文字幕免费视频网站| A级毛片高清免费视频在线播放| 亚洲乱码中文字幕小综合| 亚洲国产成人影院播放| 在线观看www日本免费网站| 老司机午夜性生免费福利 |