zoukankan      html  css  js  c++  java
  • FreeMarker学习(宏<#macro>的使用)

    原文链接:https://my.oschina.net/weiweiblog/blog/506301?p=1

    用户定义指令-使用@符合来调用  
    有两种不同的类型:Macro(宏)和transform(传递器),Macro是在模板中使用macro指令定义,而transform是在模板外由程序定义(基本上都是基于Java的),这里通过Macro来介绍自定义指令。  
    例一:  
    <#macro greet>  
    <font size="+2">Hello Joe!</font>  
    </#macro>  
    使用:<@greet></@greet> 或 <@greet/>  
    结果:<font size="+2">Hello Joe!</font>  

    参数-在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>  

    macro可以有多个参数,参数的次序是无关的,在macro指令中只能使用定义的参数,并且必须对所有参数赋值,可以在定义参数时指定缺省值:  

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


    在自定义指令嵌套内容:模板片断中使用<#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.  

    注意:嵌套内容是无法访问到macro中的局部变量的。  
    例如:  

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


    下面是一个嵌套使用自定义指令的例子:  

    <@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中使用循环变量-作为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!  

    注意:循环变量和用户定义指令开始标记指定的数目可以不同,调用时少指定循环变量,则多指定的值不可见,调用时多指定循环变量,多余的循环变量不会被创建。  

    模板中的变量,有三种类型:  
    1.) plain(全局)变量:可以在模板的任何地方访问,包括使用include指令插入的模板,使用assign指令创建和替换  
    2.) 局部变量:在macro中有效,使用local指令创建和替换  
    3.) 循环变量:只能存在于指令的嵌套内容,由指令(如list)自动创建;宏的参数是局部变量,而不是循环变量  

    用assign指令创建和替换的例子:  

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

    局部变量隐藏(而不是覆盖)同名的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 -->  
    命名(namespaces)空间-通常情况,只使用一个命名空间,称为主命名空间(main namespace),但你是不会意识到这些的;为了创建可重用的macro、transforms或其它变量的集合(通常称库),必须使用多命名空间,为了防止同名冲突。  

    首先创建一个库(假设保存在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指令中指定的hash(散列)变量访问库中的变量:  

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

  • 相关阅读:
    js回调函数
    axios如何先请求A接口然后在请求B接口
    蓝桥杯省赛 区间移位(二分+玄学贪心)
    P1403 [AHOI2005]约数研究(筛法)
    P1029 最大公约数和最小公倍数问题(数论水题)
    洛谷P1147连续自然数和(前缀和)
    洛谷P1017进制转换(进制转换/取模)
    洛谷P1088火星人(stl/全排列)
    Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round) D. Navigation System(最短路)
    Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round) C. Remove Adjacent(贪心+暴力)
  • 原文地址:https://www.cnblogs.com/zhongshiqiang/p/5993481.html
Copyright © 2011-2022 走看看