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 callper.getName()$per.age→ Attempts to callper.getAge()
Your entity:
- Private fields:
name,age - Public properties start with uppercase:
Name,Age, corresponding methodsGetName(),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:
| Variable | Function |
|---|---|
$foreach.index | Current index, starts at 0 |
$foreach.count | Current sequence number, starts at 1 |
$foreach.first | True if it is the first record (boolean) |
$foreach.last | True 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
#foreach(variable in collection)+#endis Velocity standard loop syntax;- For ordinary List iteration, output
$variabledirectly; - Iterate entity objects:
$object.propertyNamematch property case‑sensitivity, never access private fields; - C# entities should use uppercase first‑letter for properties, keep consistent naming in templates;
- When data shows blank, first check property case and existence of get accessor.
Iterate collections
Previous: Comments