[Manual] Repeat a block of items in your emails using for loops

Brevo offers two methods for repeating a block of items in your emails: using a Dynamic content block in the drag-and-drop email editor or typing for loops manually.
This article covers the manual method. If you prefer using a Dynamic content block, check our dedicated article Understand the dynamic content block.

Use a for loop to dynamically display all elements in an array. This is especially useful when the number of items in the array is unknown, such as a list of weekly blog posts or a receipt for purchased products.

You can use a for loop based on different criteria:

  • Event data
    Personalize content based on actions your contacts have (or haven’t) taken. For example, you could show a promotional block only to contacts who have never placed an order.
  • Data feeds
    Personalize content using real-time data from external feeds. For example, you can show personalized product recommendations, live pricing, or localized store information based on external data.
  • Product feeds
    Personalize content using using product information from your ecommerce website. For example, you can show your best seller products, or personalized product recommendations.

What is a for loop?

A for loop is a programming structure used to repeat a block of code for each item in an array. Instead of manually referencing each index, the loop iterates through the array and displays each item automatically. 

In email templates, for loops allow you to dynamically display content such as:

  • A receipt listing all the items a customer has purchased.
  • A reminder email showing the items left in a customer’s cart.
  • An RSS campaign displaying the latest blog articles.
  • A personalized email with up-to-date product recommendations from an external data feed.

Structure and syntax of a for loop

The syntax of a for loop can vary based on the type of element being repeated. In our examples, replace placeholder, array, and variable with the appropriate data for the element you want to repeat, as explained in the following table:

  Event data Data feed Product feed
placeholder

Use the exact name of the array in your JSON structure, but in its singular form

For data feeds, the placeholder is always item. For product feeds, the placeholder is always item.
array

Use params.array and replace array with the exact name of the array in your JSON structure.

Use feedname.array and replace:

  • feedname with the name of your data feed alias.
  • arraywith the exact name of the array in your JSON structure.

Use feed.productfeedname.products and replace productfeedname with the name of your product feed as displayed in the Product feeds page

variable

Use the corresponding event variable to display the desired element.

To learn more, check our dedicated section in the article [Manual] Personalize your messages with dynamic content (Brevo Template Language)

Use the corresponding data feed variables as they are displayed in your external data source file.

Use the corresponding product feed variables as they are displayed in your ecommerce website. 

for, in

Displays all items in an array as a list.

Syntax
{% for placeholder in array %}
variable
{% endfor %}

Open the tab corresponding to the type of data you want to display:

Event data Data feed Product feed
Example input
{% for item in params.items %}
{{ item.name }} - {{ item.currency }}{{ item.price }}
{% endfor %}
Example output

Black shoes - $99

White shirt - $20

Red pants - $49

else

Displays a replacement block if an array is empty.

Syntax
{% for placeholder in array %}
variable
{% else %}
Add your replacement content here
{% endfor %}

Example input
{% for user in params.users %}
{{ user.username }}
{% else %}
No user found
{% endfor %}
Example output

No user found

limit

Limits the loop to the specified number of iterations.

Syntax
{% for placeholder in array limit:# %}
variable
{% endfor %}
Example input
{% for item in params.items limit:2 %}
{{ item.name }}
{% endfor %}
Example output

Product a

Product b

reversed

Reverses the print order of items in your array.

Syntax
{% for placeholder in array reversed %}
{% endfor %}
Example input
{% for country in params.countries reversed %}
{% endfor %}
Example output

Zimbabwe

Sweden

Mexico

Germany

Argentina

Counter

Sets to an integer representing the loop iteration count, starting at 1. This is useful for numbering items in a loop, for example.

Syntax
{% for placeholder in array %}
{{ forloop.Counter }}. variable
{% endfor %}
Example input
{% for product in params.products %}
{{ forloop.Counter }}. {{ product.name }}
{% endfor %}
Example output

1. Product a

2. Product b

3. Product c

Counter0

Sets to an integer representing the loop iteration count, starting at 0. 

Syntax
{% for placeholder in array %}
{{ forloop.Counter0 }}. variable
{% endfor %}
Example input
{% for product in params.products %}
{{ forloop.Counter0 }}. {{ product.name }}
{% endfor %}
Example output

0. Product a

1. Product b

2. Product c

Revcounter

Sets to an integer representing the number of items left in the loop. It starts at the total number of items and decreases to 1 on the final iteration.

Syntax
{% for placeholder in array %}
{{ forloop.Revcounter }}. variable
{% endfor %}
Example input
{% for product in params.products %}
{{ forloop.Revcounter }}. {{ product.name }}
{% endfor %}
Example output

5. Product e

4. Product d

3. Product c

2. Product b

1. Product a

Revcounter0

Sets to an integer representing the number of items left in the loop. It starts at the total number of items and decreases to 0 on the final iteration.

Syntax
{% for placeholder in array %}
{{ forloop.Revcounter0 }}. variable
{% endfor %}
Example input
{% for product in params.products %}
{{ forloop.Revcounter0 }}. {{ product.name }}
{% endfor %}
Example output

4. Product e

3. Product d

2. Product c

1. Product b

0. Product a

if

Displays a list of items based on specific conditions.

Syntax
{% for placeholder in array %}
{% if condition}
variable
{% endif %}
{% endfor %}
Example input
{% for product in params.products %}
{% if product.price > 50 %}
{{ product.name }} - ${{ product.price }}
{% endif %}
{% endfor %}
Example output

Product a - $89

Product c - $105

Product e - $54

➡️ To learn more about if statements, check our dedicated article [Manual] Show or hide content in your emails using if statements.

forloop.First

A Boolean set to True on the first loop iteration. This is useful for handling the first item differently, like applying the "upper" filter only to the first product in a list.

Syntax
{% for placeholder in array %}
{{ forloop.Counter }}.
{% if forloop.First %}
variable1
{% else %}
variable2
{% endif %}

{% endfor %}
Example input
{% for product in params.products %}
{{ forloop.Counter }}.
{% if forloop.First %}
{{ product.name|upper }}
{% else %}
{{ product.name }}
{% endif %}
{% endfor %}
Example output

1. FIRST PRODUCT

2. Second product

3. Third product

forloop.Last

A Boolean set to True on the last loop iteration.  This is useful for handling the final item differently, such as omitting a trailing comma in a list.

Syntax
{% for placeholder in array %}
variable{% if not forloop.Last %},{% endif %}
{% endfor %}
Example input
{% for product in params.products %}
{{ product.name }}{% if not forloop.Last %},{% endif %}
{% endfor %}
Example output

Product a, Product b, Product c

forloop.Parentloop

Refers to the forloop object of the parent loop in nested loops.

Syntax
{% for placeholder in array %}
Country #{{ forloop.Counter }}: variable
{% for placeholder in array %}
City #{{ forloop.Counter }}: variable
{% endfor %}
{% endfor %}
Example input
{% for country in countries %}
Country #{{ forloop.Counter }}: {{ country.name }}
{% for city in country.city_list %}
City #{{ forloop.Counter }}: {{ city }}
{% endfor %}
{% endfor %}
Example output

Country #1: USA

City #1: New York

City #2: Los Angeles

City #3: Chicago

Country #2: Germany

City #1: Berlin

City #2: Munich

City #3: Hamburg

⏭️ What's next?

🤔 Have a question?

If you have a question, feel free to contact our support team by creating a ticket from your account. If you don't have an account yet, you can contact us here.

If you’re looking for help with a project using Brevo, we can match you with the right certified Brevo Agency partner.

💬 Was this article helpful?

5 out of 17 found this helpful