Loop Syntax
Loop tags allow you to dynamically repeat content in your documents. You can use them to repeat paragraphs, table rows, table columns, and list items based on your data.
To get started, follow the examples below.
Simple example
To repeat content, simply wrap it with a loop tag. The content can include constant parts as well as other tags to be replaced with data:
{{ FruitName }} is healthy for you!
{{<<}}
Provide an array in your data to populate the loop:
{
"HealthyFood": [
{ "FruitName": "Apple" },
{ "FruitName": "Banana" },
{ "FruitName": "Orange" }
]
}
The resulting document will contain the list of fruits:
Apple is healthy for you!
Banana is healthy for you!
Orange is healthy for you!
List items
Repeating list items is a common use case. Place the loop tag inside a list item, and the service will automatically generate a new list item for each element in your data array:
- {{>> Pets }}{{ Name }} {{ Type }}{{<<}}
Consider the following data:
{
"Pets": [
{ "Name": "Max", "Type": "Dog" },
{ "Name": "Jack", "Type": "Parrot" },
{ "Name": "Wendy", "Type": "Cat" }
]
}
The resulting document will contain the list of pets:
- Max Dog
- Jack Parrot
- Wendy Cat
The same approach works for numbered lists:
- {{>> Pets }}{{ Name }} {{ Type }}{{<<}}
And the resulting document will contain the list of pets:
- Max Dog
- Jack Parrot
- Wendy Cat
Table rows
To repeat table rows, place the opening tag at the beginning of the row and the closing tag in the last cell. The service will create a new row for each item in your data array:
| Name | Type |
| {{>> Pets }}{{ Name }} | {{ Type }}{{<<}} |
We'll use the same data as in the previous example:
{
"Pets": [
{ "Name": "Max", "Type": "Dog" },
{ "Name": "Jack", "Type": "Parrot" },
{ "Name": "Wendy", "Type": "Cat" }
]
}
And the resulting document will now be:
| Name | Type |
| Max | Dog |
| Jack | Parrot |
| Wendy | Cat |
Table columns
You can also repeat table columns. Place the opening tag in the first cell of the column and the closing tag in the last cell. The service will create a new column for each item in your data array:
| Student | {{ >> Students }}{{ Name }} |
| Math | {{ Math }} |
| Science | {{ Science }} |
| History | {{ History }}{{<<}} |
Consider the following data:
{
Students: [
{
Name: "John Doe",
Math: "A",
Science: "B+",
History: "A-",
},
{
Name: "Jane Smith",
Math: "B",
Science: "A",
History: "A",
},
],
}
The resulting document will show the student's grades across subjects:
| Student | John Doe | Jane Smith |
| Math | A | B |
| Science | B+ | A |
| History | A- | A |
Tips and Tricks
Repeat multiple list items in one loop iteration
You can wrap more than one list item with the same loop, so each data item produces a group of list items:
- {{>> Pets }}Name: {{ Name }}
- Type: {{ Type }}{{<<}}
With the same Pets data from above, the output becomes:
- Name: Max
- Type: Dog
- Name: Jack
- Type: Parrot
- Name: Wendy
- Type: Cat
Repeat multiple rows in one loop iteration
The same pattern works in tables: place the opening tag in the first row of the group and the closing tag in the last row of that same group.
| Pet | Value |
| {{>> Pets }}Name | {{ Name }} |
| Type | {{ Type }}{{<<}} |
The resulting document will be:
| Pet | Value |
| Name | Max |
| Type | Dog |
| Name | Jack |
| Type | Parrot |
| Name | Wendy |
| Type | Cat |
Override loop behavior with loopOver
Default loop behavior
When the engine detects a loop, it picks what to repeat based on built-in heuristics:
- If both loop tags are inside the same table cell, it repeats the cell content.
- If both loop tags are in the same table column, it repeats the column.
- If both loop tags are inside a table (but not the same cell or column), it repeats the relevant table rows.
- If both loop tags are inside a list, it repeats list items.
- Otherwise, it repeats the content between the opening and closing tags.
Overriding the default
If you need to force a specific behavior, set the loopOver option on the
opening loop tag. There are four possible values:
row- Repeat one or more rowscolumn- Repeat a columncontent- Repeat only the content between tagsparagraph- Repeat one or more paragraphs
Example
Assume we have the following data:
{
"Students": [
{ "Name": "Alice" },
{ "Name": "Bob" }
]
}
Notice how each option produces a different output:
loopOver: "row"
Template:
| Student |
| {{ >> Students [loopOver: "row"] }}{{ Name }}{{ << }} |
Resulting document:
| Student |
| Alice |
| Bob |
loopOver: "column"
Template:
| Student |
| {{ >> Students [loopOver: "column"] }}{{ Name }}{{ << }} |
Resulting document:
| Student | Student |
| Alice | Bob |
loopOver: "content"
Template:
| Students |
| {{ >> Students [loopOver: "content"] }}{{ Name }} {{ << }} |
Resulting document:
| Students |
| Alice Bob |
loopOver: "paragraph"
Template:
| Students |
| {{ >> Students [loopOver: "paragraph"] }}{{ Name }}{{ << }} |
Resulting document:
| Students |
Alice |
loopOver works for conditions too, not only loops, and you can use it to control what content the condition hides or shows.