In this article, we will explain how to manually repeat a block of items that will automatically repeat itself and iterate over all items within a single variable.
You can repeat a block in:
- an email receipt email to display all the items your client has purchased,
- an abandoned cart email to display all the items that were left in your client's cart,
- an RSS campaign to automatically display a series of blog articles,
- etc.
Good to know
- Make sure you are familiar with using transactional parameters to customize your emails. If you're not, please refer to our dedicated article Customize your emails using transactional parameters as a first step.
What is a for-loop?
To repeat a block, you have two options:
- Use the Repeat block feature available within both Drag & Drop editors.
- (Advanced) Manually add for-loops using the for tag in your email templates.
The tag for enables you to loop, or iterate, over each item in a list. This is particularly helpful when the number of items in a list is unknown when creating an email template.
Combined with your transactional parameters, the for tag enables you to easily insert a dynamic list of items in your emails. These for-loops are structured by adding the for tag around your transactional parameters placeholders:
{% for VARIABLE in params.PARAMETERNAME %}
{{ VARIABLE.name }} - {{ VARIABLE.price }}
{% endfor %}
If you have connected your website to Brevo using one of our plugins, by default your variable and parameter names will be:
- variable: item
- parameter: items
If you are not using a Brevo plugin or if you are using other transactional parameters, you can find your variable and parameter name in your event logs. Learn how to find this data in our article Customize your emails using transactional parameters.
How are for-loops structured?
A for-loop is created using the for tag which enables you to loop, or iterate, over each item in a sequence. This is particularly helpful when the number of items in the sequence is unknown when creating an email template, like in a dynamic list.
Basic for-loop
Here are several common ways to use the for tag:
for, in |
display a list |
<ul> |
else |
render a replacement block if your sequence is empty |
<ul> Would display “no user found”. |
reversed |
reverses the print order of items in your sequence |
{% for country in params.countries reversed %} {% endfor %} Would display a list of countries, but in reverse order compared to the original list. |
Advanced for-loop
Inside of a for-loop block you can also access these special variables:
forloop.Counter |
Always set to an integer representing the number of times the loop has been entered. This is one-indexed, so the first time through the loop, forloop. Counter will be set to 1. This may be easily used to number items in a loop: {% for product in params.products %} will output a list like this: 1. Product a |
forloop.Counter0 |
The current iteration of the loop. (0 indexed) |
forloop.Revcounter |
Always set to an integer representing the number of remaining items in the loop. The first time through the loop, forloop.Revcounter will be set to the total number of items in the sequence you’re traversing. The last time through the loop, forloop.revcounter will be set to 1. |
forloop.Revcounter0 |
The number of iterations from the end of the loop (0 indexed) |
forloop.First |
Boolean value set to True if this is the first time through the loop. This is convenient for special-casing the first item in a list, such as adding the “upper” filter (described in Modify the formatting of your placeholder with filters) only to the first item in this sample list of products. {% for product in params.products %} Could output a list of products like this: 1. FIRST PRODUCT - $5.00 |
forloop.Last |
Boolean value set to True if this is the last time through the loop. A common use for this is to put separating characters (like , or |) in a list of items. |
forloop.Parentloop |
References the forloop object for the parent loop, in case of nested loops. Here’s an example: {% for country in countries %} |
Insert a for-loop in an email
In your email template, type your for-loop around your placeholders ({{ item.name }} - {{ item.price }} in our example below).
|
Here's how your email can look like once populated with the data of a recipient who purchased 4 items:
🤔 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 expert partner.