freemark的list遍历内置函数详解

基于freemarker 2.3.28

1. index 返回列表的索引从0开始

template:

<#list ['a', 'b', 'c'] as x>
  ## 写成x_index也可以
  ${x?index}
</#list>

output:

0
1
2

2. counter 计数器 索引从1开始

template:

<#list ['a', 'b', 'c'] as i>
${i?counter}: ${i}
</#list>

output:

1: a
2: b
3: c

3. has_next 是否还有下一个

最后一个时返回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

4. is_even_item 是否是偶数索引

第0 ,2 , 4…个时返回true

template:

<#list ['a', 'b', 'c', 'd'] as i>${i?is_even_item?c} </#list>

output:

false true false true

5.is_odd_item 是否是奇数索引

template:

<#list ['a', 'b', 'c', 'd'] as i>${i?is_odd_item?c} </#list>

output:

true false true false 

6. is_first 是否是第一个

第一个返回true

template:

<#list ['a', 'b', 'c'] as i>${i?is_first?c} </#list>

output:

true false false 

7. is_last 是否是最后一个

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>
<tr class="${i?item_parity}Row">${i}</tr>
</#list>

output:

  <tr class="oddRow">a</tr>
  <tr class="evenRow">b</tr>
  <tr class="oddRow">c</tr>
  <tr class="evenRow">d</tr>

9. item_parity_cap 返回“Odd”或者“Even”

item_parity的区别: 返回的首字母大写 “Odd”或者“Even”

template:

<#list ['a', 'b', 'c', 'd'] as i>
<tr class="row${i?item_parity_cap}">${i}</tr>
</#list>

output:

  <tr class="rowOdd">a</tr>
  <tr class="rowEven">b</tr>
  <tr class="rowOdd">c</tr>
  <tr class="rowEven">d</tr>

10. item_cycle 自定义循环索引

template:

<#list ['a', 'b', 'c', 'd', 'e', 'f', 'g'] as i>
<tr class="${i?item_cycle('row1', 'row2', 'row3')}">${i}</tr>
</#list>

output:

  <tr class="row1">a</tr>
  <tr class="row2">b</tr>
  <tr class="row3">c</tr>
  <tr class="row1">d</tr>
  <tr class="row2">e</tr>
  <tr class="row3">f</tr>
  <tr class="row1">g</tr>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一本郑经

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值