Advanced - Repeat a block of items using for-loops

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

What is a for-loop?

To repeat a block, you have two options:

  1. Use the Repeat block feature available within both Drag & Drop editors.
  2. (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 Sendinblue using one of our plugins, by default your variable and parameter names will be:

  • variable: item
  • parameter: items

If you are not using a Sendinblue 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>
   {% for user in params.USERS %}
       <li>{{ user.username }}</li>
   {% endfor %}
</ul>

else

render a replacement block if your sequence is empty

<ul>
   {% for user in params.USERS %}
       <li>{{ user.username }}</li>
   {% else %}
       <li><em>no user found</em></li>
   {% endfor %}
</ul>

Would display “no user found”.

reversed

reverses the print order of items in your sequence

{% for country in params.countries reversed %} {% end for %}

Would display a list of countries, but in reverse order compared to the original list.

 

Advanced for-loop

💡 Good to know
Loop variable names are case sensitive.

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 %}
{{ forloop.Counter }}. {{ product.name }} {% endfor %}

will output a list like this:

1. Product a
2. Product b
3. Product c, etc.

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 %}
{{ forloop.Counter }}. {% if forloop.First %} {{ product.name|upper }} {% else %} {{ product.name }} {% endif %} - {{ product.price }}
{% endfor %}

Could output a list of products like this:

1. FIRST PRODUCT - $5.00
2. Second product - $2.00
3. Third product - $4.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 %}
<table>
{% for city in country.city_list %}
<tr>
<td>Country #{{ forloop.Parentloop.counter }}</td>
<td>City #{{ forloop.Counter }}</td>
<td>{{ city }}</td>
</tr>
{% endfor %}
</table>
{% endfor %}

 

 

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).






{% for item in params.items %}

{{ item.name }} - {{ item.price }}
{% endfor %}





mceclip5.png

Here's how your email can look like once populated with the data of a recipient who purchased 4 items: 

mceclip4.png