(三) Freemarker 常用指令

来源:互联网 发布:arm软件侵权赔偿 编辑:程序博客网 时间:2024/05/16 23:51

一 常用指令

      1. assign: 自定义变量

      2. if else :流程判断指令

      3. list: 变量sequence 

      4. marco: 宏指令(自定义方法)

      5. import: 引入命名空间

      6. include:  嵌入文件指令


二 指令详解

【1. assign 指令】: 自定义变量
    1.1   自定义变量:
        <#assign hw = "hello,world"/>

    1.2  调用:用$ 调用
        hw = ${hw}
    
【2. if 指令】: 判断语句, if 相关的指令中不需要用 $ 号,直接可以从map 中获取元素
    2.1 if else 组合:
        <#if status == 0>
            status is 0
        <#else>
            status is not 0
        </#if>

    2.2 if elseif else 组合:
        <#if status ==0 >
            status = 0
        <#elseif status ==1>
            status = 1 
        <#else>
            status != 0 && status !=1
        </#if>
    
【3. list 指令】:list可以遍历sequence 类型的数据, 即 数组, List, Set
    1.1 foreach:
        <#list citys as city>
            ${city_index} --> ${city}
        </#list>

    3.2   获取遍历索引: 变量名_index 
        <#list names as name>
            ${name_index} --> ${name}
        </#list>

【4. macro 宏指令】, 类似于自定义方法
    1. 定义宏指令(定义方法)
        <#macro sayHello name1 name2>
            hello, ${name1} and ${name2}
        </#macro>

    2. 调用宏指令(调用方法)
        <@sayHello name1="zhangsan" name2="lisi"/>
    
    3. nested 指令: 代替调用宏指令时标签对之间包裹的内容
        <#macro hw>
            hello,<#nested>
        </#macro>
        <@hw>zhangsan,lisi,wangwu,zhaoliu</@hw>
        
【5. import 指令】: 引入命名空间:
    1. 引入命名空间:<#import "templateUtil.ftl" as util />
    2. 调用响应方法:<@util.printEmp employee/>
    3. 调用变量:${util.fileName}
        修改变量:<#assign fileName = "command.ftl" in util />
               ${util.fileName}
            
【6. include 】嵌入文件:
    1. 执行步骤: 
        1. 合并文件: 将被嵌入的文件的内容全部导入当前 文件
        2. 解析: 然后用freemarker 语法解析嵌入的文件片段
    2. 示例:以下是文件 hello.ftl 的文本内容:
       <#include "include.txt" />
    

三 测试结果:

[html] view plain copy
  1. 二 指令详解  
  2.   
  3. 【1. assign 指令】: 自定义变量  
  4.     1.1   自定义变量:  
  5.         <#assign hw = "hello,world"/>  
  6.     1.2  调用:用$ 调用  
  7.         hw = ${hw}  
  8.       
  9. 【2. if 指令】: 判断语句, if 相关的指令中不需要用 $ 号,直接可以从map 中获取元素  
  10.     2.1 if else 组合:  
  11.         <#if status == 0>  
  12.             status is 0  
  13.         <#else>  
  14.             status is not 0  
  15.         </#if>  
  16.     2.2 if elseif else 组合:  
  17.         <#if status ==0 >  
  18.             status = 0  
  19.         <#elseif status ==1>  
  20.             status = 1  
  21.         <#else>  
  22.             status != 0 && status !=1  
  23.         </#if>  
  24.       
  25. 【3. list 指令】:list可以遍历sequence 类型的数据, 即 数组, List, Set  
  26.     1.1 foreach:  
  27.         <#list citys as city>  
  28.             ${city_index} --> ${city}  
  29.         </#list>  
  30.     3.2   获取遍历索引: 变量名_index  
  31.         <#list names as name>  
  32.             ${name_index} --> ${name}  
  33.         </#list>  
  34.   
  35. 【4. macro 宏指令】, 类似于自定义方法  
  36.     1. 定义宏指令(定义方法)  
  37.         <#macro sayHello name1 name2>  
  38.             hello, ${name1} and ${name2}  
  39.         </#macro>  
  40.     2. 调用宏指令(调用方法)  
  41.         <@sayHello name1="zhangsan" name2="lisi"/>  
  42.       
  43.     3. nested 指令: 代替调用宏指令时标签对之间包裹的内容  
  44.         <#macro hw>  
  45.             hello,<#nested>  
  46.         </#macro>  
  47.         <@hw>zhangsan,lisi,wangwu,zhaoliu</@hw>  
  48.           
  49. 【5. import 指令】: 引入命名空间:  
  50.     1. 引入命名空间:<#import "templateUtil.ftl" as util />  
  51.     2. 调用响应方法:<@util.printEmp employee/>  
  52.     3. 调用变量:${util.fileName}  
  53.         修改变量:<#assign fileName = "command.ftl" in util />  
  54.                ${util.fileName}  
  55.               
  56. 【6. include 】嵌入文件:  
  57.     1. 执行步骤:  
  58.         1. 合并文件: 将被嵌入的文件的内容全部导入当前 文件  
  59.         2. 解析: 然后用freemarker 语法解析嵌入的文件片段  
  60.     2. 示例:以下是文件 hello.ftl 的文本内容:  
  61.        <#include "include.txt" />  
原创粉丝点击