Adding dynamic lists to your DocuGenerate templates is an excellent way to organize and display repetitive data in an easy-to-read format. With DocuGenerate, you can loop over arrays in your JSON data to create lists. Arrays can contain primitive data types or nested objects.
If your JSON data contains an array of primitive data, such as strings or numbers, you can create a list by using a simple loop structure in your template.
Here’s an example of the template syntax:
[#items]
- [.]
[/items]
In this example, [#items]
and [/items]
denote the start and end of the loop. You can also use [/]
as a shorthand version to denote the end of the loop. The [.]
within the loop is where each item from the array will be placed.
For instance, if your JSON data is as follows:
[{
"items": [
"Cars",
"Trains"
]
}]
The generated list will look like this in your document:
- Cars
- Trains
You can also loop over an array of nested objects. In this case, you can access each object’s properties within the loop.
Here’s an example of the template syntax:
[#items]
- [name]: [quantity]
[/items]
In this example, [#items]
and [/items]
again denote the start and end of the loop. The [name]: [quantity]
within the loop is where each item’s name and quantity properties will be placed.
For instance, if your JSON data is as follows:
[{
"items": [{
"name": "Cars",
"quantity": "10"
}, {
"name": "Trains",
"quantity": "20"
}]
}]
The generated list will look like this in your document:
- Cars: 10
- Trains: 20
In summary, adding lists to your templates allows you to efficiently display repetitive data, whether they are primitive types or nested objects.