FreeMark学习(二)


(1)用户定义指令

宏和变换器变量是两种不同类型的用户定义指令,它们之间的区别是宏是在模板中使用macro指令定义,而变换器是在模板外由程序定义,这里只介绍宏

基本用法

 宏是和某个变量关联的模板片断,以便在模板中通过用户定义指令使用该变量,下面是一个例子:

<#macro greet>
  <font size="+2">Hello Joe!</font>
</#macro>  
作为用户定义指令使用宏变量时,使用@替代FTL标记中的#

<@greet></@greet>
如果没有体内容,也可以使用:

<@greet/>
参数

在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"/>就正确了

宏的参数是局部变量,只能在宏定义中有效

 

(2)嵌套内容

用户定义指令可以有嵌套内容,使用<#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: ? ? ?
          

在宏定义中使用循环变量

用户定义指令可以有循环变量,通常用于重复嵌套内容,基本用法是:作为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!
  
指定的循环变量的数目和用户定义指令开始标记指定的不同不会有问题

 调用时少指定循环变量,则多指定的值不可见

 调用时多指定循环变量,多余的循环变量不会被创建

(2)在模板中定义变量

         在模板中定义的变量有三种类型:

         plain变量:可以在模板的任何地方访问,包括使用include指令插入的模板,使用assign指令创建和替换

         局部变量:在宏定义体中有效,使用local指令创建和替换

         循环变量:只能存在于指令的嵌套内容,由指令(如list)自动创建;宏的参数是局部变量,而不是循环变量

         局部变量隐藏(而不是覆盖)同名的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
 
内部循环变量隐藏同名的外部循环变量,如:

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


(3)名字空间

通常情况,只使用一个名字空间,称为主名字空间

为了创建可重用的宏、变换器或其它变量的集合(通常称库),必须使用多名字空间,其目的是防止同名冲突

创建库

下面是一个创建库的例子(假设保存在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指令导入库到模板中,Freemarker会为导入的库创建新的名字空间,并可以通过import指令中指定的散列变量访问库中的变量:

<#import "/lib/my_test.ftl" as my>
<#assign mail="fred@acme.com">
<@my.copyright date="1999-2002"/>
${my.mail}
${mail}  
输出结果:

<p>Copyright (C) 1999-2002 Julia Smith. All rights reserved.
<br>Email: jsmith@acme.com</p>
jsmith@acme.com
fred@acme.com  
可以看到例子中使用的两个同名变量并没有冲突,因为它们位于不同的名字空间

可以使用assign指令在导入的名字空间中创建或替代变量,下面是一个例子:

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

jsmith@acme.com
jsmith@other.com 

 
数据模型中的变量任何地方都可见,也包括不同的名字空间,下面是修改的库:

<#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>
Fred@acme.com   
 

 

 
Freemarker - 几个比较实用的例子
                                     

用Freemarker做模本语言有一段时间了,列出几个和JSP或者Velocity相比起来比较方便的用途,目的是引诱更多的人跳上Freemarker这个贼船, 

1. String内置的JavaScript转换: js_string 
用途:用于JavaScript转义,转换',",换行等特殊字符 
模板: 

<script> 
alert("${errorMessage?js_string}"); 
</script> 


输出: 

<script> 
alert("Readonly\'s pet name is \"Cross Bone\""); 
</script> 


2.内置的默认值处理:default 
用途: 用于处理默认值 
模本: 

User: ${userLogin.name?default("Anonymous")} 

<td>${(employee.department.manager.name)?default(" ")}</td> 


输出: 

User: Anonymous 
<td> </td> 


注,可以对整个对象树加上(),再用内置处理器这种方便的做法,偶也是最近刚学会的,以前一直用很傻的方法做..... 

3. Sequence内置的计数器: xxx_index 
用途:显示序号 
模板: 

<#list employees as e> 
${e_index}. ${e.name} 
</#list> 


输出: 

1. Readonly 
2. Robbin 


4. Sequence内置的分段器: chunk 
用途:某些比较BT的排版需求 
模板: 

<#assign seq = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']> 
<#list seq?chunk(4) as row> 
<ul> 
<li><#list row as cell>${cell} </#list></li> 
</ul> 
</#list> 

<#list seq?chunk(4, '-') as row> 
<tr> 
<td><#list row as cell>${cell} </#list></td> 
</tr> 
</#list> 


输出: 

<ul> 
<li>a</li> 
<li>b</li> 
<li>c</li> 
<li>d</li> 
</ul> 
<ul> 
<li>e</li> 
<li>f</li> 
<li>g</li> 
<li>h</li> 
</ul> 
<ul> 
<li>i</li> 
<li>j</li> 
</ul> 

<tr> 
<td>a</td> 
<td>b</td> 
<td>c</td> 
<td>d</td> 
</tr> 
<tr> 
<td>e</td> 
<td>f</td> 
<td>g</td> 
<td>h</td> 
</tr> 
<tr> 
<td>i</td> 
<td>j</td> 
<td>-</td> 
<td>-</td> 
</tr> 

posted @ 2016-04-01 17:43  牧师963  阅读(242)  评论(0编辑  收藏  举报