您的位置:首页 > 其它

freemarker 自定义指令

2014-07-14 10:56 155 查看
1 简介

自定义指令可以使用 macro 指令来定义,这是模板设计者所关心的内容。 Java 程序员若不想在模板中实 现定义指令 ,而是在 Java 语言中实现指令 的定义,这时 可以使用freemarker.template.TemplateDirectiveModel 类来扩展

2 基本内容

macro 指令自身不打印任何内容,它只是用来创建宏变量,所以就会有一个名为greet 的变量。在 <#macro greet> 和 </#macro> 之间的内容(称为宏定义体)当使用它作为指令时将会被执行。

<#macro greet>
<font size="+2">Hello Joe!</font>
</#macro>
可以在 FTL 标记中通过 @ 代替 # 来使用自定义指令。使用变量名作为指令名。
<@greet></@greet>
<@greet/>


3 参数
宏名称的后面位置是用来定义变量的。
<#macro greet person>
<font size="+2">Hello ${person}!</font>
</#macro> 那么就可以这样来使用这个宏:
<@greet person="Fred"/> and <@greet person="Batman"/>
使用预定义指令时,参数的值( = 号后边的值)可以是 FTL 表达式。这样,不像 HTML, "Fred"和 "Batman" 的引号就可以不用要了。 <
@greet person=Fred/> 也意味着使用变量的值 Fred 作为 person 参数,而不是字符串 "Fred" 。
也可以在 = 号左边使用复杂表达式(比如someParam=(price + 50)*1.25 )。

可以有多个参数。

<#macro greet person color>
<font size="+2" color="${color}">Hello ${person}!</font>
</#macro>
宏就可以这样来使用:
<@greet person="Fred" color="black"/>
<@greet color="black" person="Fred"/>

你仅仅可以使用在 macro 指令中定义的参数,同 时 也 必 须 给 出 在 宏 中 定 义 所 有 参 数 的 值 。

指定默认值:
<#macro greet person color="black">
<font size="+2" color="${color}">Hello ${person}!</font>
</#macro><@greet person="Fred"/> ,因为它和<@greet person="Fred" color="black"/> 是相同的
如 果 想 给 color 设 置 为 ”red” , 那 么 就 写 成 : <@greet person="Fred" color="red"/>

someParam=foo 和someParam="${foo}" 是不同的。如果指令需要 someParam是一个数字值,那么就不要用第二种方式。

4 嵌套内容

<#macro border>
<table border=4 cellspacing=0 cellpadding=4><tr><td>
<#nested>
</td></tr></table>
</#macro><#nested> 指令执行位于开始和结束标记指令之间的模板代码段。如果这样写:
<@border>The bordered text</@border>那么就会输出:
<table border=4 cellspacing=0 cellpadding=4><tr><td>
The bordered text
</td></tr></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!"?"} ${x!"?"} ${count!"?"}</@repeat>
test 3/1: ? ? ?
test 3/2: ? ? ?
test 3/3: ? ? ?
局部变量的设置是为每个宏自己调用的
<#macro test foo>${foo} (<#nested>) ${foo}</#macro>
<@test foo="A"><@test foo="B"><@test foo="C"/></@test></@test>
A (B (C () C) B) A


5 宏和循环变量

循环变量的名字是已经给定的,变量值的设置是由指令本身完成的。
<#macro do_thrice>
<#nested 1>
<#nested 2>
<#nested 3>
</#macro>
<@do_thrice ; x> <#-- 用户自定义指令 使用";"代替"as" -->
${x} Anything.
</@do_thrice>
1 Anything.
2 Anything.
3 Anything.
语法规则是为特
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.
2.
3.
4.
0.5
1
1.5
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>如果在分号后面指定了比 nested 指令还多的变量,那么最后的循环变量将不会被创建(在嵌套内容中不会被定义)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: