基于freemarker 2.3.28
template:
<#list ['a', 'b', 'c'] as x>
## 写成x_index也可以
${x?index}
#list>
output:
0
1
2
template:
<#list ['a', 'b', 'c'] as i>
${i?counter}: ${i}
#list>
output:
1: a
2: b
3: c
最后一个时返回false 其它的为true
template:
<#list ['a', 'b', 'c'] as i>${i?has_next?c} #list>
output:
true true false
当不得不显示介于每个迭代项(但不能在第一项之前或最后一项之后) 之间的一些内容时,可以使用
sep
更方便。例如:
templae:
<#list users as user>${user}<#sep>,#sep> #list>
output:
Joe, Kate,Fred
第0 ,2 , 4…个时返回true
template:
<#list ['a', 'b', 'c', 'd'] as i>${i?is_even_item?c} #list>
output:
false true false true
template:
<#list ['a', 'b', 'c', 'd'] as i>${i?is_odd_item?c} #list>
output:
true false true false
第一个返回true
template:
<#list ['a', 'b', 'c'] as i>${i?is_first?c} #list>
output:
true false false
template:
<#list ['a', 'b', 'c'] as i>${i?is_last?c} #list>
output:
false false true
##8. item_parity 返回“odd”或者“even”
这个是基于第一个索引是1开始的,返回的是小写字符串
template:
<#list ['a', 'b', 'c', 'd'] as i>
${i}
#list>
output:
a
b
c
d
和item_parity
的区别: 返回的首字母大写 “Odd”或者“Even”
template:
<#list ['a', 'b', 'c', 'd'] as i>
${i}
#list>
output:
a
b
c
d
template:
<#list ['a', 'b', 'c', 'd', 'e', 'f', 'g'] as i>
${i}
#list>
output:
a
b
c
d
e
f
g