I'm working on an email template in Klaviyo, and I'm using {{ event.Items|default:'' }} to display a list of items from multiple orders. However, the preview shows the list with square brackets and commas, like this:
How's Your Experience with ['Product 1', 'Product 2', 'Product 3']?
I'd like to format this list without the square brackets and ideally add the word 'and' between multiple items, like this:
How's Your Experience with Product 1, Product 2, and Product 3?
Can anyone please advise on how to achieve this formatting using Klaviyo? Any help would be greatly appreciated!
This works and achieves what you want. Add this as a text block as shown.
<div> How's Your Experience with {% if event|lookup:'Item Count' > 1 %} {% for item in event.extra.line_items %} {% if not forloop.last %}{{ item.title }}, {% else %}and {{ item.title }}? {% endif %} {% endfor %} {% else %} {{ event.Items.0 }}? {% endif %} </div>
Notes:
The reason for the first if’ statement is so the formatting is correct for instances where there’s only one product
The first ‘if’ statement checks to see if there is more than one product. If there is, the next few lines loop over the products and adds a comma between them, apart from the last product in the list, which is preceded by ‘and’.
If there is only one product, that is displayed without any formatting.
You need to enclose the code between <div>...</div> as shown above; otherwise, Klaviyo will insert divs and break the formatting.
This works and achieves what you want. Add this as a text block as shown.
<div> How's Your Experience with {% if event|lookup:'Item Count' > 1 %} {% for item in event.extra.line_items %} {% if not forloop.last %}{{ item.title }}, {% else %}and {{ item.title }}? {% endif %} {% endfor %} {% else %} {{ event.Items.0 }}? {% endif %} </div>
Notes:
The reason for the first if’ statement is so the formatting is correct for instances where there’s only one product
The first ‘if’ statement checks to see if there is more than one product. If there is, the next few lines loop over the products and adds a comma between them, apart from the last product in the list, which is preceded by ‘and’.
If there is only one product, that is displayed without any formatting.
You need to enclose the code between <div>...</div> as shown above; otherwise, Klaviyo will insert divs and break the formatting.
It works!!! 🎉 I've added the text block as instructed, and everything looks great. Your detailed notes are super helpful for understanding the logic behind the code. I really appreciate your guidance. Looking forward to being part of the community!