FreeMarker设计指南(5)

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

可以看到例子中使用的两个同名变量并没有冲突,因为它们位于不同的名字空间

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

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

l         输出结果:

jsmith@acme.com
     
     
jsmith@other.com  
     
     

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>
     
     
Fred@acme.com   
     
     
<script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/buttonLite.js#style=-1&uuid=&pophcol=3&lang=zh"></script> <script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/bshareC0.js"></script>
阅读(599) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值