Skip to main content
Contributor I
April 13, 2024
Solved

event.Items issue - square bracket

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

    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