Use the repetition option to render a collection of items in an email based on a list of data.
Tabular's repetition option can be used to repeat an email block based on personal client data, for example to show a list of blog posts or ecommerce products based on an array/list of data.
For-loops can be used to iterate through sequences of objects. They are most commonly used in email templates for rendering blog posts, ecommerce products, or purchase order line items.
To be able to use for-loops, your email service provider should offer a templating language with for-loop support.
The idea is that the block that you wish to repeat (for example an ecommerce product block), should be wrapped in a for-loop. Wrapping a block, means that before the block we add a for-loop opening expression, and after the block we add a for-loop closing expression.
The templating language used differs per email service provider, but for example the HTML when using the Handlebars templating language, would look like the following:
<ul class="people_list">
{{#each people}}
<li>{{this}}</li>
{{/each}}
</ul>
{% for item in items %}
, where items should be replaced by the variable name for your array/list. With the Handlebars language it would look like: {{#each items}}
.{% endfor %}
. With the Handlebars language it would look like: {{/each}}
Inside the block that has the repetition item set, you can access data related to the element being iterated over by the for-loop syntax. You can use it on the block itself and any blocks that are nested in it.
Please note that the template language used is only supported by some email service providers, however, the concept of how we show to use it in Tabular is applicable to other templating languages. We have listed in a table at the end of this article which template language each email service provider supports.
Say we have the following data that we want to iterate over:
{
products: [
{ name: "Kitchen Block", price: 30.00 },
{ name: "Pants", price: 39.00 },
{ name: "T-Shirt", price: 20.00 }
]
}
For iterating, We add the repetition item to the Row block that we wish to render for each product in our products list:
Then, inside the block, we can use this
to reference the element being iterated over. In this case, we can use {{ this.name }}
to access the product name, and {{ this.price }}
to access the product price. We add these tags and mark them as variables using Tabular's variable annotation:
When we send this email with an email service provider that supports the Handlebar language, the resulting email would look like:
Each uniquely personalized email will need to be automatically generated and during this the variables used together with the for-loop will be substituted with client data.
This process of substituting the variables with the client data is typically performed by your email service provider (ESP). Your ESP should then also have access to that client data.
Some ESPs may offer functionalities to manually add the client data or they might collect the client data. Other ESPs may offer integrations, so that you can connect the database where you've stored the client data, with the ESP, so that the ESP can fetch the client data from your database. If you programmatically send emails using the ESP's API, most API's offer endpoints that you can use to send variable values to the ESP.
Another option if you have developers in your team, is to do this process of substitution on your own backend server, and then sending the HTML using the API of an ESP.
Most email service providers offer a templating language, see the following table that lists the most commonly used ESPs and if they offer for-loop support:
Email service provider | Link to documentation | For-loop support |
---|---|---|
HubSpot | HubL Templating Language | ✅ HubSpot For-loop syntax |
Mailchimp | Merge Tags | ❌ No support |
Mailchimp Transactional | Handlebars and Merge Tags | ✅ Mailchimp Handlebars For-loop syntax |
Postmark | Mustachio | ✅ Mustachio Collection Handling |
Crisp | Handlebars | ✅ #each |
Mailgun | Handlebars | ✅ #each |
Brevo | Handlebars | ✅ #each |
Klaviyo | Content repeat feature | ✅ #each |
SendGrid | Using Handlebars | ✅ #each |
MailerLite | Variables in MailerLite | ❌ No support |
Mailjet | Mailjet Templating Language | ✅ For-loops |
Auth0 | Using Liquid Syntax | ✅ For loops |
Moosend | ❌ No support | ❌ No support |
VBOUT | ❓ No documentation | ❓ No documentation |
ZeptoMail | Dynamic Templates | ✅ Collection iteration |
SendPulse | Clever personalization | ❓ No documentation |
Acumbamail | ❓ No documentation | ❓ No documentation |
Sendy | ❓ No documentation | ❓ No documentation |
Transpond | Merge Tags | ❌ No support |
Encharge | Personalizing with Liquid | ✅ For loops |