测试代码

    @Test
    public void test07(){
        try {
            root.put("name", "张三");
            freemakerUtil.fprint(root, "07.ftl", fn+"07.html");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 flt模版文件

<body>
    <#--自定义指令
        1、自定义指令 如果使用必须要调用 <@ />这种形式调用
        2、自定义指令可以设置参数,参数可以设置默认值,
              设置了默认值的参数在调用的时候可以不设置值,
              设置默认值的参数必须放在最后面
3、如果自定义指令没定义参数,那么在调用的时候也不能传,否则会报错 -->
<#--自定义指令循环打印5次--> <#macro hello world num=5> <#list 1..num as n> hello : ${name}-[${world}]-${n} </#list> </#macro> <@hello world="你好"/> <#--可以用nested替换里面的内容--> <#macro test num=3> <ul> <#list 1..num as n> <#nested n/> </#list> </ul> </#macro> <@test;n> <li>${n}.abc</li> </@test> <#--在自定义指令中使用assign定义变量会改变模版中的值,是全局变量,应该使用local来定义--> <#macro helloworld> <#--<#assign name="李四"/>--> <#local name="李四"> ${name} </#macro> <@helloworld/> ${name} </body>

效果:

 【freemaker】之自定义指令<#macro>

 

相关文章:

  • 2022-12-23
  • 2021-07-13
  • 2021-09-12
猜你喜欢
  • 2021-09-08
  • 2022-12-23
  • 2021-05-19
  • 2021-07-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案