Skip to main content
Contributor I
April 13, 2024
Solved

event.Items issue - square bracket

  • April 13, 2024
  • 3 replies
  • 239 views

Hello everyone,

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!

Thank you!

    This topic has been closed for replies.
    Best answer by bluesnapper

    Welcome to the community @laycocupo 

    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.

    Hope that helps

    Regards

    Andy

    3 replies

    bluesnapper
    Partner
    Champion & Partner
    April 14, 2024

    Welcome to the community @laycocupo 

    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.

    Hope that helps

    Regards

    Andy

    https://bluesnapper.com - Klaviyo Champion '24 & Partner
    laycocupoAuthor
    Contributor I
    April 14, 2024

    Thanks for the warm welcome, @bluesnapper! :)

    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!

    -Layco

    bluesnapper
    Partner
    Champion & Partner
    April 14, 2024

    Happy to help @laycocupo and thanks for letting me know it worked for you.

    Regards

    Andy

    https://bluesnapper.com - Klaviyo Champion '24 & Partner