Freemarker for loop

Do they have any way to move the list item based on them, and not one at a time? I want to view a list of fields in 1,3,5,7,9 and 2,4,6,8 orders. 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> 

But that gave me a mistake.

+4
source share
1 answer

This is what ?chunk 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 do not know what error you will get with your decision, but, of course, there is an error in that it displays all the fields except the last one twice. You can freely play with 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 can see, this really is not suitable for FreeMarker (this is terribly verbose).

+5
source

Source: https://habr.com/ru/post/1492080/


All Articles