Freemarker For loop

对着背影说爱祢 提交于 2019-12-05 17:49:05

This is what ?chunk is for (http://freemarker.org/docs/ref_builtins_sequence.html#ref_builtin_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>
...

but as you see it doesn't really fit FreeMarker (it's awfully verbose).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!