How can I show just part of an event variable that’s around 4-5 words? For example, the product title variable {{ event.extra.line_items.0.product.title }} is 6 words but I only want to show the first 3.
Hi
Thanks for sharing this question with the Klaviyo Community.
You can use the “slice” filter to limit how many items from the list are displayed. This is a Django template language filter which you can learn more about here: Built-in template tags and filters | Django documentation | Django.
You apply this filter to the value in the Row Collection field in the Rows > Data Source section of the table block.
Since you want to limit the list to only showing the first 3 items and the value is event.extra.line_items
it should then be changed to event.extra.line_items|slice:'3'
where the number, in this case 3, is how many items you would like to display.
Thank you and have a great weekend.
Hi, just an additional question: is there a way to dynamically do this for strings? In some cases, would like to include 3 words from a string, in other cases, would like to include 4 words.
Hello
Yes, but since the slice filter works slightly differently for string data (limits the number of characters) vs list data (limits the number of line-items in the array), you need to use a separate filter called truncatewords. This filter also automatically adds an ellipsis and a space to the truncated string. Therefore, you need to also include the slice tag to remove both the space and the ellipsis from the end of the string. For example, if I wanted to limit the item title to one word, I would use “1” for truncate words and I would always use slice “-4” to get rid of the ellipsis and the space that would otherwise follow that word. So, the tag would look like the following:
{{item.product.title|truncatewords:1|slice:"-4"}}
This should be inserted directly in the source code:
The result is just a single word in the title of the product(s). The full title of this product is “Chocolate River Bar” now reduced to just “Chocolate”.
This can be done for any dynamic variable in the source code.
Hello
Yes, but since the slice filter works slightly differently for string data (limits the number of characters) vs list data (limits the number of line-items in the array), you need to use a separate filter called truncatewords. This filter also automatically adds an ellipsis and a space to the truncated string. Therefore, you need to also include the slice tag to remove both the space and the ellipsis from the end of the string. For example, if I wanted to limit the item title to one word, I would use “1” for truncate words and I would always use slice “-4” to get rid of the ellipsis and the space that would otherwise follow that word. So, the tag would look like the following:
{{item.product.title|truncatewords:1|slice:"-4"}}
This should be inserted directly in the source code:
The result is just a single word in the title of the product(s). The full title of this product is “Chocolate River Bar” now reduced to just “Chocolate”.
This can be done for any dynamic variable in the source code.
Thanks, Dov! This is very helpful. Sorry just an additional question, what is the filter for “truncatewords” if I want to hide the first 5 words?
Hi
Glad that was helpful for you!
Since Klaviyo uses Django syntax for our templates, I’d recommend reviewing built-in template tags and filters here to find the exact tags you’re looking for. I also recommend browsing our help center article on template tags and variable syntax for more commonly used variables.
Thanks for being a member of the community!
Reply
Log in to the Community
Use your Klaviyo credentials
Log in with Klaviyo
Use your Klaviyo credentials
Log in with KlaviyoEnter your E-mail address. We'll send you an e-mail with instructions to reset your password.