Syntax:< #if ></#if > Background transfer list, the foreground HTML page gets the list and displays:
<#ifuserlist?exists> <#listUserlistas Item> <TR> <TD>${ITEM_INDEX?IF_EXISTS+1}</TD>//Gets the loop subscript value, which is starting from 0 by default. <TD>${item.username?if_exists}</TD> </TD> </#list> </#if>
It is very convenient and most commonly used to display list contents in a template page:
1. If there is a bean class users,users has name, age and other properties,list<users> users as background data,
< #list users as user>
<span>${user.name}</span>
<span>${user.age}</span></br>
</#list >
2. The users section of the above example can also be defined in the field, as
< #list [1,2,3,4] as Index>
<span>${index}</span>
</#list >
The result is shown as: 1234
3. If you just want to count it, you can write it
< #list 1..4 as index>
<span>${index}</span>
</#list >
The result is the same as above, and it's 1234.
If you need to display the current loop to the first item, you can write this
< #list ["Hello", "Welcome", "HI"] as word>
<span>${word_index+1},${word}</span></br>
</#list >
The variable after the AS, plus _index, can represent the current loop to the first item
The result is:
1,hello
2,welcome
3,hi
Sometimes, the last item may have to be handled at the time of display, how to judge the last item?
< #list ["Hello", "Welcome", "HI"] as word>
<span>${word}</span>< #if word_has_next>,</#if ></#list >
As the variable behind the AS, plus _has_next, you can determine whether the last item
The result is:
Hello,welcome,hi
You can do this if you want to exit from a loop in which an item is determined
< #list users as user>
<span>${user.name}</span>
< #if User.Name = = "Pxx" >< #break ></#break >
</#list >
List app for Freemark