Using Filters to Customize Template Tags

read
Last updated at:

Overview

Filters allow you to customize the format and content of variables in the messages you send. For a list of filters available to use in the template editor, reference Klaviyo's variable filters glossary. In this article, you will learn more about how filters work and how to use them to customize template tags.

Before Starting

Before you get started, become familiar with how Klaviyo’s template tags work. In order to use filters on variables, you’ll need to be able to identify the correct base variables and add them to your template. 

How Filters Work

Filters are applied to template tags/variables to adjust the variable that displays. Some possible use cases are:

  • Apply consistent capitalization to a piece of text (for example, you can use the upper filter to take any variable and make it all uppercase)
  • Show the price for an item after a promotion has been applied using the multiply filter 
  • Set the number of decimal places to display for a number variable, using the floatformat filter

For a list of supported filters, reference our variable filters glossary

Apply a Filter to a Variable

To apply a filter to a template tag, the following elements should be added:

  • A pipe symbol ( | ) after the variable name
  • The filter name 
  • If the filter takes any arguments (i.e., additional parameters or inputs), add a colon, followed by the argument(s) 

Do not add in any additional spaces. Here is an example of a template tag with a filter applied:

{{ item.price|floatformat:2 }}

In this example, item.price is the variable name, floatformat is the filter, and 2 is the argument required by the filter. 

If the argument is a piece of text, it needs to be surrounded by single quotation marks. Quotation marks are not needed if the argument is a number. Reference Klaviyo’s variable filters glossary for examples. 

Tips for Using Filters

If you’re copying filters and pasting them into your template, make sure to paste as plain text to avoid pasting in formatting along with the filter itself. To paste as plain text, use the paste as plain text keyboard shortcut (PC: Ctrl+Shift+V / Mac: Cmd+Shift+V), or use the plain text pasting option in the editor. 

paste_as_plain_text.png

It’s important to ensure the data referenced in your variables is appropriate for the filter being used. Some filters can only be applied to string (text) data types, while others only work on lists or numbers. If the filter isn’t working as expected, review the event or profile data you’re using to ensure it is the correct type. 

Applying Multiple Filters

If needed, you can apply multiple filters to a single variable. To do so, connect each filter using a pipe symbol ( | ), and consider that the filters will generally be applied in order (from first to last). Here are some examples of variables with multiple filters applied: 

{{ first_name|title|default:'there' }}

The first filter applies title casing to the recipient’s first name, and the second filter supplies a default word to be used if no first name has been provided. 

{{ item.price|multiply:.8|floatformat:2 }}

The first filter multiplies the base item price by .8 (to show the price if a 20% off coupon has been applied), and the second filter specifies that two decimal places should appear. 

Additional Resources

x
Was this article helpful?
2 out of 3 found this helpful