赋值语句:
<#assign b = 2>
if语句:
<#if>主体部分</#if>
<#if a??>、<#if a?exists>判断a是否为空
循环语句:
<#list 要循环的 as 循环个体>主体部分</#list>
<#list Key as k>
循环时循环体下标为:k_index
是否有下一个:k_has_next
变量格式:
${XXX},但使用在<#assign>、<#list>、<#if>时只需要写XXX
字符串操作
字符串连接:可以直接嵌套${"hello , ${name}"} ; 也可以用加号${"hello , " + name}
字符串截取:string[index]。index 可以是一个值,也可以是形如 0..2 表示下标从0开始,到下标为2结束。一共是三个数。
比较运算符
== (等于),!= (不等于),gt(大于),gte(大于或者等于),lt(小于),lte(小于或者等于)。不建议用 >,< 可能会报错!
一般和 if 配合使用
内置函数:
格式为:变量?内置函数
首字母大写:${string?cap_first}
所有字母小写:${string?lower_case}
所有字母大写:${string?upper_case}
数值取整数:${floatdata?int}
集合长度:${users?size}
时间格式化:${dataTime?string("yyyy-MM-dd")}
Map集合:
<#assign mapData={"name":"程序员", "salary":15000}>
通过Key获取 Value值:${mapData["name"]}
遍历Key值:<#list mapData?keys as key>
遍历Value值:<#list mapData?values as value>
List集合:
<#assign listData=["ITDragon", "blog", "is", "cool"]>
<#list listData as value>${value} </#list>
去第i个元素:${list[i]}
将字符串变为list,再取第i个元素:${(str?split(","))[i]}
list排序遍历:<#list list?sort_by("字段") as x>
逆序遍历:<#list list?sort_by("字段")?reverse as x>
include指令:
引入其他文件:<#include "otherFreeMarker.ftl" />
macro宏指令:
<#macro mo>
定义无参数的宏macro--${name}
</#macro>
使用宏macro: <@mo />
<#macro moArgs a b c>
定义带参数的宏macro-- ${a+b+c}
</#macro>
使用带参数的宏macro: <@moArgs a=1 b=2 c=3 />
命名空间:
<#import "otherFreeMarker.ftl" as otherFtl>
${otherFtl.otherName}
<@otherFtl.addMethod a=10 b=20 />
<#assign otherName="修改otherFreeMarker.ftl中的otherName变量值"/>
${otherFtl.otherName}
<#assign otherName="修改otherFreeMarker.ftl中的otherName变量值" in otherFtl />
${otherFtl.otherName}