Freemarker语法杂项

97 阅读1分钟

if-else结构

<#if condition>
...
<#elseif condition2>
...
<#elseif condition3>
...
<#else>
...
</#if>


switch结构 (条件可为数字,也可为字符串)

<#switch value>
 <#case refValue1>
 ....
<#break> 
<#case refValue2> 
....
<#break>
 <#case refValueN>
 ....
<#break>
 <#default>
 ....
 </#switch>


遍历map

<#list map?keys as k>
	${k}
	${map[k]}
</#list>


日期输出

${user.createTime?string('yyyy-MM-dd HH:mm:ss')}
如果日期可能为nulll,写法为:${(user.createTime?string('yyyy-MM-dd HH:mm:ss'))!}

blog.csdn.net/qq_33348497…



三目运算

${(flag==1)?  string('aaa','bbb')}"


字符串拼接

${"hello${user.name!}"} 	// 输出hello+变量名
${"hello"+user.name!} 		// 使用+号来连接,输出hello+变量名


字符串截取

<#assign str = "abcdefghijklmn"/>

${str?substring(0,4)} 	// 显示: abcd
${str[0]}${str[4]} 		// 显示: ae
${str[1..4]}     	  // 显示: bcde


返回指定字符的索引

<#assign str = "abcdefghijklmn"/>

${str?index_of("d")}  	// 显示: 3

逻辑运算符

大于、小于、大于等于、小于等于: gt, lt , gte, lte