docs/docs/en/template-print/syntax/loops.md
Loop handling is used to repeatedly render data from arrays or objects by defining start and end markers for the loop. Below, several common scenarios are described.
{d.array[i].property} to define the current loop item, and use {d.array[i+1].property} to specify the next item to mark the loop area.[i] part) is automatically used as the template for repetition; you only need to write the loop example once in the template.Example syntax format:
{d.arrayName[i].property}
{d.arrayName[i+1].property}
{
"cars": [
{ "brand": "Toyota", "id": 1 },
{ "brand": "Hyundai", "id": 2 },
{ "brand": "BMW", "id": 3 },
{ "brand": "Peugeot","id": 4 }
]
}
Carsid
{d.cars[i].brand}{d.cars[i].id}
{d.cars[i+1].brand}
Carsid
Toyota1
Hyundai2
BMW3
Peugeot4
Suitable for cases where an array contains nested arrays; nesting can be at an infinite level.
[
{
"brand": "Toyota",
"models": [
{ "size": "Prius 4", "power": 125 },
{ "size": "Prius 5", "power": 139 }
]
},
{
"brand": "Kia",
"models": [
{ "size": "EV4", "power": 450 },
{ "size": "EV6", "power": 500 }
]
}
]
{d[i].brand}
Models
{d[i].models[i].size} - {d[i].models[i].power}
{d[i].models[i+1].size}
{d[i+1].brand}
Toyota
Models
Prius 4 - 125
Prius 5 - 139
Kia
Bidirectional loops allow iteration over both rows and columns simultaneously, which is suitable for generating comparison tables and other complex layouts (note: currently, some formats are officially supported only in DOCX, HTML, and MD templates).
{
"titles": [
{ "name": "Kia" },
{ "name": "Toyota" },
{ "name": "Hopium" }
],
"cars": [
{ "models": [ "EV3", "Prius 1", "Prototype" ] },
{ "models": [ "EV4", "Prius 2", "" ] },
{ "models": [ "EV6", "Prius 3", "" ] }
]
}
{d.titles[i].name}{d.titles[i+1].name}
{d.cars[i].models[i]}{d.cars[i].models[i+1]}
{d.cars[i+1].models[i]}
KiaToyotaHopium
EV3Prius 1Prototype
EV4Prius 2
EV6Prius 3
Within a loop, you can directly access the current iteration's index, which helps meet special formatting requirements.
{d[i].cars[i].other.wheels[i].tire.subObject:add(.i):add(..i):add(...i)}
Note: The number of dots indicates the index level (for example,
.irepresents the current level, while..irepresents the previous level). There is currently an issue with reverse ordering; please refer to the official documentation for details.
.att to obtain the property name and .val to obtain the property value.Example syntax format:
{d.objectName[i].att} // property name
{d.objectName[i].val} // property value
{
"myObject": {
"paul": "10",
"jack": "20",
"bob": "30"
}
}
People namePeople age
{d.myObject[i].att}{d.myObject[i].val}
{d.myObject[i+1].att}{d.myObject[i+1].val}
People namePeople age
paul10
jack20
bob30
Using the sorting feature, you can directly sort array data within the template.
{d.array[sortingAttribute, i].property}
{d.array[sortingAttribute+1, i+1].property}
{
"cars": [
{ "brand": "Ferrari", "power": 3 },
{ "brand": "Peugeot", "power": 1 },
{ "brand": "BMW", "power": 2 },
{ "brand": "Lexus", "power": 1 }
]
}
Cars
{d.cars[power, i].brand}
{d.cars[power+1, i+1].brand}
Cars
Peugeot
Lexus
BMW
Ferrari
{
"cars": [
{ "brand": "Ferrari", "power": 3, "sub": { "size": 1 } },
{ "brand": "Aptera", "power": 1, "sub": { "size": 20 } },
{ "brand": "Peugeot", "power": 1, "sub": { "size": 20 } },
{ "brand": "BMW", "power": 2, "sub": { "size": 1 } },
{ "brand": "Kia", "power": 1, "sub": { "size": 10 } }
]
}
Cars
{d.cars[power, sub.size, i].brand}
{d.cars[power+1, sub.size+1, i+1].brand}
Cars
Kia
Aptera
Peugeot
BMW
Ferrari
Filtering is used to filter out rows in a loop based on specific conditions.
age > 19). The syntax format is:
{d.array[i, condition].property}
[
{ "name": "John", "age": 20 },
{ "name": "Eva", "age": 18 },
{ "name": "Bob", "age": 25 },
{ "name": "Charly", "age": 30 }
]
People
{d[i, age > 19, age < 30].name}
{d[i+1, age > 19, age < 30].name}
People
John
Bob
{d.array[i, type='rocket'].name}
[
{ "name": "Falcon 9", "type": "rocket" },
{ "name": "Model S", "type": "car" },
{ "name": "Model 3", "type": "car" },
{ "name": "Falcon Heavy","type": "rocket" }
]
People
{d[i, type='rocket'].name}
{d[i+1, type='rocket'].name}
People
Falcon 9
Falcon Heavy
i to filter out the first N elements. For example:
{d.array[i, i < N].property}
[
{ "name": "Falcon 9" },
{ "name": "Model S" },
{ "name": "Model 3" },
{ "name": "Falcon Heavy" }
]
People
{d[i, i < 2].name}
{d[i+1, i < 2].name}
People
Falcon 9
Model S
i to represent items from the end. For example:
{d.array[i=-1].property} retrieves the last item.{d.array[i, i!=-1].property} excludes the last item.[
{ "name": "Falcon 9" },
{ "name": "Model S" },
{ "name": "Model 3" },
{ "name": "Falcon Heavy" }
]
Last item: {d[i=-1].name}
Excluding the last item:
{d[i, i!=-1].name}
{d[i+1, i!=-1].name}
Excluding the last two items:
{d[i, i<-2].name}
{d[i+1, i<-2].name}
Last item: Falcon Heavy
Excluding the last item:
Falcon 9
Model S
Model 3
Excluding the last two items:
Falcon 9
Model S
Example format:
{d.array[property].property}
{d.array[property+1].property}
[
{ "type": "car", "brand": "Hyundai" },
{ "type": "plane", "brand": "Airbus" },
{ "type": "plane", "brand": "Boeing" },
{ "type": "car", "brand": "Toyota" }
]
Vehicles
{d[type].brand}
{d[type+1].brand}
Vehicles
Hyundai
Airbus