本文介绍了Freemarker For循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
他们是否可以根据自己而不是一个一个地遍历列表项目?我想以1,3,5,7,9和2,4,6,8顺序遍历一系列字段。我尝试使用这样的
Is their any way to traverse list item based on their rather than one by one? I want to traverse a list of fields in 1,3,5,7,9 and 2,4,6,8 order. I tried using like this
<#list section.field as field>
<div class="col1">
${field.@label}:<input type="text"/></div>
<#if field_has_next>
<div class="col2">
${field[field_index+1].@label}:<input type="text"/>
</div>
</#if>
</#list>
但它给了我错误。
推荐答案
这是?chunk
适用于():
<#list section.field?chunk(2) as row>
<#list row as field>
<div class="col${field_index + 1}">
${field.@label}: <input type="text"/>
</div>
</#list>
</#list>
否则我不知道你的解决方案有什么错误,但肯定有错误它显示所有字段,但最后一次显示两次。您可以使用类似
Otherwise I don't know what error you get with your solution, but surely there's a bug in it that it displays all fields but the last one twice. You could freely play with the indexes with something like
<#assign fields = section.field>
<#assign idx = 0>
<#list 0..999999 as _>
<#if idx == fields?size><#break></#if>
... even column ...
<#assign idx = idx + 1>
<#if idx == fields?size><#break></#if>
... odd column ...
<#assign idx = idx + 1>
</#list>
...
但是你认为它并不适合FreeMarker(它很糟糕)详细)。
but as you see it doesn't really fit FreeMarker (it's awfully verbose).
这篇关于Freemarker For循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!