Loops

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:

{{>> HealthyFood }}
{{ FruitName }} is healthy for you!
{{<<}}

Provide an array in your data to populate the loop:

This preview shows the tag fields as they appear in the TemplateDocs UI.

The resulting document will contain the list of fruits:

Apple is healthy for you!
Banana is healthy for you!
Orange is healthy for you!

Paragraphs

To repeat one or more complete paragraphs, place each loop tag alone in its own paragraph, before and after the paragraphs to repeat:

{{>> Jobs }}
{{ Title }}, {{ Company }}
{{ Description }}
{{<<}}

Consider the following data:

This preview shows the tag fields as they appear in the TemplateDocs UI.

The paragraphs between the loop tags are repeated for each item, and the paragraphs of the loop tags themselves are removed, so they leave no empty lines in the document:

Senior Designer, Acme
Led the redesign of the product line.
Designer, Globex
Built the design system.

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:

This preview shows the tag fields as they appear in the TemplateDocs UI.

The resulting document will contain the list of pets:

  • Max Dog
  • Jack Parrot
  • Wendy Cat

The same approach works for numbered lists:

  1. {{>> Pets }}{{ Name }} {{ Type }}{{<<}}

And the resulting document will contain the list of pets:

  1. Max Dog
  2. Jack Parrot
  3. 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:

NameType
{{>> Pets }}{{ Name }}{{ Type }}{{<<}}

We'll use the same data as in the previous example:

This preview shows the tag fields as they appear in the TemplateDocs UI.

And the resulting document will now be:

NameType
MaxDog
JackParrot
WendyCat

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:

This preview shows the tag fields as they appear in the TemplateDocs UI.

The resulting document will show the student's grades across subjects:

StudentJohn DoeJane Smith
MathAB
ScienceB+A
HistoryA-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.

PetValue
{{>> Pets }}Name{{ Name }}
Type{{ Type }}{{<<}}

The resulting document will be:

PetValue
NameMax
TypeDog
NameJack
TypeParrot
NameWendy
TypeCat

Customizing loop behavior

Default loop behavior

When the engine detects a loop, it picks what to repeat based on built-in heuristics:

  1. If both loop tags are inside the same table cell, it repeats the cell content.
  2. If both loop tags are in the same table column, it repeats the column.
  3. If both loop tags are inside a table (but not the same cell or column), it repeats the relevant table rows.
  4. If both loop tags are inside a list, it repeats list items.
  5. If each loop tag is alone in its own paragraph and the paragraph loops setting is on, it repeats the paragraphs between the tags.
  6. Otherwise, it repeats the content between the opening and closing tags.

The repeat option

If you need to force a specific behavior, set the repeat option on the opening loop tag. Options are written in square brackets after the tag name, for example {{ >> Students [repeat: "row"] }}. There are four possible values:

  • row - Repeat one or more rows
  • column - Repeat a column
  • content - Repeat only the content between tags
  • paragraph - Repeat one or more paragraphs

Example

Assume we have the following data:

This preview shows the tag fields as they appear in the TemplateDocs UI.

Notice how each option produces a different output:

repeat: "row"

Template:

Student
{{ >> Students [repeat: "row"] }}{{ Name }}{{ << }}

Resulting document:

Student
Alice
Bob
repeat: "column"

Template:

Student
{{ >> Students [repeat: "column"] }}{{ Name }}{{ << }}

Resulting document:

StudentStudent
AliceBob
repeat: "content"

Template:

Students
{{ >> Students [repeat: "content"] }}{{ Name }} {{ << }}

Resulting document:

Students
Alice Bob
repeat: "paragraph"

Template:

Students
{{ >> Students [repeat: "paragraph"] }}{{ Name }}{{ << }}

Resulting document:

Students

Alice
Bob

The paragraph loops setting

For new accounts, the setting is on by default. When it is off, only the content between the loop tags is repeated and the paragraphs of the loop tags stay in the document as empty lines.

Considering the following template:

{{>> Students }}
{{ Name }}
{{<<}}

When the setting is on, the output is:

Alice
Bob

When the setting is off, the output becomes:


Alice


Bob


You can turn it on or off in the organization settings:

  1. Open Settings and go to the Organization tab
  2. In the Templates section, toggle Repeat whole paragraphs in loops

You can also change it for a single template, whatever the organization setting is:

  1. Open the template and go to the Preferences tab
  2. Under Repeat whole paragraphs in loops, choose On or Off. Choose Use organization setting to follow the organization again.