天天看點

Freemarker定義宏的用法

Freemarker宏 

一.什麼是宏:macro 

引用 宏是在模闆中使用macro指令定義 

基本用法 

宏是和某個變量關聯的模闆片斷,以便在模闆中通過使用者定義指令使用該變量, 

Java代碼  

Freemarker定義宏的用法
  1. <#macro greet>     
  2.      <font size="+2">Hello Joe!</font>     
  3. </#macro>    
  4. <#macro greet>  
  5.       <font size="+2">Hello Joe!</font>  
  6. </#macro>  

調用宏時,與使用FreeMarker的其他指令類似,隻是使用@替代FTL标記中的#。 

<@greet></@greet> <#--<@greet/>--> 

在macro指令中可以在宏變量之後定義參數,如: 

Java代碼  

Freemarker定義宏的用法
  1. <#macro greet person>     
  2.     <font size="+2">Hello ${person}!</font>     
  3. </#macro>    
  4. <#macro greet person>  
  5.      <font size="+2">Hello ${person}!</font>  
  6. </#macro>  

可以這樣使用這個宏變量: 

<@greet person="Fred"/> 

但是下面的代碼具有不同的意思: 

<@greet person=Fred/> 

這意味着将Fred變量的值傳給person參數,該值不僅是字元串,還可以是其它類型,甚至是複雜的表達式。 

宏可以有多參數,下面是一個例子: 

Java代碼  

Freemarker定義宏的用法
  1. <#macro greet person color>     
  2.  <font size="+2" color="${color}">Hello ${person}!</font>     
  3. </#macro>    
  4.  <#macro greet person color>  
  5.     <font size="+2" color="${color}">Hello ${person}!</font>  
  6.   </#macro>  

可以這樣使用該宏變量,其中參數的次序是無關的: 

引用   <@greet person="Fred" color="black"/>

引用     可以在定義參數時指定預設值,否則,在調用宏的時候,必須對所有參數指派: 

<#macro greet person color="black"> 

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

  </#macro> 

注意:宏的參數是局部變量,隻能在宏定義中有效。

嵌套内容 

FreeMarker的宏可以有嵌套内容,<#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> 

FMPP 輸出結果: 

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> 

  <table border=4 cellspacing=0 cellpadding=4><tr><td>

引用 宏定義中的局部變量對嵌套内容是不可見的,例如: 

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

引用 在宏定義中使用循環變量 

nestted指令也可以有循環變量(循環變量的含義見下節),調用宏的時候在宏指令的參數後面依次列出循環變量的名字,格式如下: 

<@ macro_name paramter list; loop variable list[,]> 

例如: 

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

這裡count是宏的參數,c, halfc,last則為循環變量,輸出結果: 

1. 0.5 

  2. 1 

  3. 1.5 

  4. 2 Last!

引用 循環變量和宏标記指定的不同不會有問題,如果調用時少指定了循環變量,那麼多餘的值不可見。調用時多指定了循環變量,多餘的循環變量不會被建立: 

<@repeat count=4 ; c, halfc, last> 

    ${c}. ${halfc}<#if last> Last!</#if> 

  </@repeat> 

  <@repeat count=4 ; c, halfc> 

    ${c}. ${halfc} 

  </@repeat> 

  <@repeat count=4> 

    Just repeat it... 

  </@repeat>

在模版中定義變量 

在模闆中定義的變量有三種類型: 

引用

plain變量:可以在模闆的任何地方通路,包括使用include指令插入的模闆,使用assign指令建立和替換。 

局部變量:在宏定義體中有效,使用local指令建立和替換。 

循環變量:隻能存在于指令的嵌套内容,由指令(如list)自動建立;宏的參數是局部變量,而不是循環變量 

局部變量隐藏(而不是覆寫)同名的plain變量;循環變量隐藏同名的局部變量和plain變量,下面是一個例子: 

<#assign x = "plain"> 

  ${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 

内部循環變量隐藏同名的外部循環變量,如: 

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

模闆中的變量會隐藏(而不是覆寫)資料模型中同名變量,如果需要通路資料模型中的同名變量,使用特殊變量global,下面的例子假設資料模型中的user的值是Big Joe: 

<#assign user = "Joe Hider"> 

   ${user}          <#-- prints: Joe Hider --> 

   ${.globals.user} <#-- prints: Big Joe -->

引用 名字空間 

通常情況,隻使用一個名字空間,稱為主名字空間,但為了建立可重用的宏、變換器或其它變量的集合(通常稱庫),必須使用多名字空間,其目的是防止同名沖突 

建立庫 

下面是一個建立庫的例子(假設儲存在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] 

可以看到例子中使用的兩個同名變量并沒有沖突,因為它們位于不同的名字空間。還可以使用assign指令在導入的名字空間中建立或替代變量,下面是一個例子: 

<#import "/lib/my_test.ftl" as my> 

  ${my.mail} 

  <#assign mail="[email protected]" in my> 

  ${my.mail} 

輸出結果: 

  [email protected] 

  [email protected] 

資料模型中的變量任何地方都可見,也包括不同的名字空間,下面是修改的庫: 

<#macro copyright date> 

    <p>Copyright (C) ${date} ${user}. All rights reserved.</p> 

  </#macro> 

  <#assign mail = "${user}@acme.com"> 

假設資料模型中的user變量的值是Fred,則下面的代碼: 

<#import "/lib/my_test.ftl" as my> 

  <@my.copyright date="1999-2002"/> 

  ${my.mail} 

輸出結果: 

<p>Copyright (C) 1999-2002 Fred. All rights reserved.</p> 

  [email protected]

繼續閱讀