Iterate collections

Velocity #foreach Iterate over Collections & Objects

1. Iterate over String List

C# Back‑end Code
List<string> strList = new List<string>() {"dsd","sdfsd","asdfsd","asdfsdfsd"};
context.Put("strList", strList);Code language: C# (cs)
Velocity Template Code
#foreach($str in $strList)
    $str <br/>
#endCode language: PHP (php)

Execution result: Loop through each string in the list and output with line breaks.

Syntax Explanation

#foreach(loopVariable in collection) starts the loop, #end closes the loop.

2. Iterate over List of Custom Entity Classes

1. Person Entity (C#)
public class Person
{
    private string name;
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    private int age;
    public int Age
    {
        get { return age; }
        set { age = value; }
    }
    public Person(){}
    public Person(string name,int age)
    {
        this.name = name;
        this.age = age;
    }
}Code language: C# (cs)
2. Back‑end Assemble List
List<Person> pList = new List<Person>();
pList.Add(new Person("Jack", 1));
pList.Add(new Person("Ben", 11));
pList.Add(new Person("Kevin", 100));
pList.Add(new Person("Simon", 500));
pList.Add(new Person("Lulu", 20));
context.Put("pList", pList);Code language: PHP (php)
3. Template Property Access Syntax

Template code:

#foreach($per in $pList)
<tr>
    <td>$per.name</td>
    <td>$per.age</td>
</tr>
#endCode language: HTML, XML (xml)

Cannot read data! Errors or blank output

Underlying Principle

Velocity rules for accessing object properties:
$per.xxx automatically looks for public Get method GetXxx()

  • $per.name → Attempts to call per.getName()
  • $per.age → Attempts to call per.getAge()

Your entity:

  • Private fields: name, age
  • Public properties start with uppercase: Name, Age, corresponding methods GetName(), GetAge()

✅Correct template syntax:

#foreach($per in $pList)
<tr>
    <td>$per.Name</td>
    <td>$per.Age</td>
</tr>
#endCode language: HTML, XML (xml)

Note: C# convention uses uppercase first‑letter for properties; Java entity properties are usually lowercase. This is a common case‑sensitivity pitfall for beginners.

3. Built‑in Variables for #foreach

Variables available directly inside loops:

VariableFunction
$foreach.indexCurrent index, starts at 0
$foreach.countCurrent sequence number, starts at 1
$foreach.firstTrue if it is the first record (boolean)
$foreach.lastTrue if it is the last record (boolean)

Example:

#foreach($per in $pList)
    $foreach.count , Name: $per.Name
#endCode language: PHP (php)

4. Render as HTML Table

<table border="0" cellpadding="0" cellspacing="0">
#foreach($per in $pList)
    <tr>
        <td>$per.Name</td>
        <td>$per.Age</td>
    </tr>
#end
</table>Code language: HTML, XML (xml)

Summary

  1. #foreach(variable in collection) + #end is Velocity standard loop syntax;
  2. For ordinary List iteration, output $variable directly;
  3. Iterate entity objects: $object.propertyName match property case‑sensitivity, never access private fields;
  4. C# entities should use uppercase first‑letter for properties, keep consistent naming in templates;
  5. When data shows blank, first check property case and existence of get accessor.

Iterate collections

Leave a Reply

Your email address will not be published. Required fields are marked *