语法:<#/>or<#></#>
条件语句:
<#if xxx=xxx>${xxx}
<#else if ddd=ddd>${ddd}
</#if>
循环语句,遍历map
<#list options?keys as key>
str[${key_index}]='${options[key]}'+",'${key}'";
</#list>
循环语句,遍历list
<#list options as key>
${key}
</#list>
循环语句,遍历list 排序
升序
<#list options?sort as key>
${key}
</#list>
指定升序
<#list options?sort_by("name") as key>
${key}
</#list>
降序
<#list options?sort_by("name")?reverse as key>
${key}
</#list>
map的values转成list
<#assign keys=mapObject?values>
<#list keys as key>
.
</#list>
map的keys转成list
<#assign keys=mapObject?keys>
<#list keys as key>
.
</#list>
用现场定义的,如
<#list [1,2,3,4] as index>
<span>${index}</span>
</#list>
结果显示是:1234
如果只是想要计数,也可以这样写
<#list 1..4 as index>
<span>${index}</span>
</#list>
如果需要显示当前循环到第几项,可以这样写
<#list ["hello","welcome","hi"] as word>
<span>${word_index+1},${word}</span></br>
</#list>
as 后面的那个变量,加上_index,就可以表示当前循环到第几项
结果是:
1,hello
2,welcome
3,hi
有时候,最后一项在显示的时候可能要做特殊处理,怎么判断最后一项?
<#list ["hello","welcome","hi"] as word>
<span>${word}</span><#if word_has_next>,</#if></#list>
as 后面的那个变量,加上_has_next,就可以判断是否最后一项
结果是:
hello,welcome,hi
如果想在循环中判断到某一项时退出,可以这样做
<#list users as user>
<span>${user.name}</span>
<#if user.name == "pxx"><#break></#break>
</#list>