Template Tags and Variable Syntax

Overview

When creating email templates, there are a few different ways to customize content so your emails look great and are relevant to your recipients.

Klaviyo provides certain template variables that you can use to personalize email content. The most commonly used variables are personalization variable, which allow you to dynamically include information about your email recipient. For example, their name, email address, or custom properties you've stored about the person.

Hi {{ first_name }} {{ last_name }}, your email is {{ email }} Hi George Washington, your email is george.washington@example.com 


Similarly, you can also populate flow emails with event variables. When your flow is triggered by customer behavior or an event, you can dynamically populate your emails with information from this event. This is how you can send emails to customers that feature items abandoned in a cart, recently purchased items, etc.

You can also use different template tags that will produce important links or information. These tags are useful when you want to insert unsubscribe links, share links, dates and more. Other tags allow you to create content that is only shown if certain conditions are met or allow you to create loops to iterate over content.

{% manage_preferences %} | {% web_view %} Update preferences | View in your browser


Finally, Klaviyo uses template filters that are used together with template variables to provide additional functionality. This includes accessing custom properties for recipients, default values, and additional formatting options.

Hey{{ first_name|default:'friend' }}, any interest in some{{ person|lookup:'Favorite Food' }}?

Hey friend, any interest in some Hawaiian pizza with extra anchovies? 

Personalization Variables 

Because Klaviyo focuses on people and what they're up to, personalization is built into Klaviyo's core and making emails more relevant to reach recipient is easy. There are a number of different variables you can use:

First Name {{ first_name }} ex. George
Last Name {{ last_name }} ex. Washington
Email Address {{ email }} ex. george.washington@example.com
Phone Number {{ person.phone_number }} ex. 617-555-5555
Organization Name {{ person.organization }}  ex. Klaviyo
Customer Title {{ person.title }}  ex. Customer Specialist


For any variable, you can add a  default filter to provide a placeholder if there is no variable available to pull in:

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


Custom Properties

If you've added extra properties to individual contacts in Klaviyo, you can use them as dynamic content in an email with the person variable and the lookup filter. It's usually a good idea to also provide a default value in case someone doesn't have the custom property you're trying to use by including the default filter.

Your favorite color is {{ person|lookup:'Favorite Color' }} . Your favorite color is blue.
Your favorite TV show is {{ person|lookup:'Favorite TV Show'|default:'unknown' }} . Your favorite TV show is unknown.
Your birthday is on {{ person.Birthday|datetime_from_string }} .  Your birthday is on Oct. 14, 1985, 8 p.m.

 

Account Variables 

Account variables allow you include information about your company or organization in your email. You can manage this information on your account page.

You received this message from {{ organization.name }} . You received this message from the United States of America.
Our address is {{ organization.full_address }} .  Our address is 1600 Pennsylvania Avenue Washington D.C. 20500. 


Event Variables 

For flows triggered by an event (Checkout Started, Placed Order, etc.), you can use this event information within the email template.

For example, when a flow is triggered by the Placed Order event, you can thank someone for their purchase and reference the specific item they bought. Klaviyo automatically stores detailed event information for you so that you can use the appropriate variable to add dynamic event content. The information captured is specific to each metric.

When you preview a flow email, you will see all the available properties for that event in the event dialog window that pops up.

If you click on a line of data in the preview window, a small bar will appear at the bottom of the window. This bar will populate with the variable tag that corresponds with the line of data you clicked. You can copy and paste this tag directly into your template. If you are interested in looping through several values for a property (for example, populating a list of all items in a purchase), consider using the block repeat feature.

2017-05-03_14-59-54.png


For example:

644808


If you're sending events to Klaviyo via the API, all of the properties for a given event are available through the event variable and lookup filter.

Thanks for your purchase of ${{ event|lookup:'total_price' }} . Thanks for your purchase of $29.00.
How do you like your new {{ event|lookup:'Items Purchased' }} ?  How do you like your new blue suede shoes?

 

Unsubscribe, Manage Preferences, Web View

List and campaign tags allow recipients to manage their subscription preferences and view your campaign in their browser.

Unsubscribe

Klaviyo requires an unsubscribe link in all campaigns. To include an unsubscribe link, you can use the unsubscribe tag. By default, the link will have the text "Unsubscribe." If you want to customize the text in the link, this tag takes an optional parameter that allows you to customize the text. If you need even more control, there is also a tag that provides only the URL for the unsubscribe link.

{% unsubscribe %} Unsubscribe
If you'd no longer like to receive emails, {% unsubscribe 'click here' %} . If you'd no longer like to receive emails,  click here.
This is a fancy <a href="{% unsubscribe_link %}" >unsubscribe</a> link. This is a fancy unsubscribe link.


When a recipient clicks the unsubscribe link, they're taken to a page to confirm their unsubscribe request. You can see everyone who has unsubscribed in the People section of Klaviyo.

Manage Preferences

For standard lists, you can create a preferences page and customize it to match your brand. This allows recipients to update their preferences. To include a link to this page for each recipient, use the manage preferences tag. If you need more control, there is also a version that just provides the URL.

{% manage_preferences %} Manage Preferences
Want to update your preferences? {% unsubscribe 'click here' %} Want to update your preferences? Click here.
This is a fancy <a href="{% manage_preferences_link %}" >manage preferences</a> link. This is a fancy manage preferences link.


Web View

To give recipients a link to a web page that has your campaign, you can use the web view tag. Like the unsubscribe tag, if you need more control, there is also a version that just provides the URL.

{% web_view %} View in Your Browser
Can't see this email? {% web_view 'Open in your browser' %} . Can't see this email?  Open in your browser.
This is a fancy <a href="{% web_view_link %}" >web view</a>  link. This is a fancy web view link. 

Date Tags 

Date tags give you a quick way to insert the the day a campaign is sent into an email. The date is in the timezone of your account. 

Current Day of Month {% current_day %} ex. The current day of the month is 5.
Current Day of Week {% current_weekday %} ex. The current day of the week is Friday.
Current Month {% current_month_name %} The current month is September.
Current Year {% current_year %}  ex. The current year is 2017.


Currently, we only support English names for months and weekdays.

Parse property as date

The datetime_from_string parses and converts a string to a date so you can use the date filter to format it.

Birthday {% person.Birthday|datetime_from_string|date %} ex. Your birthday is on Oct. 14,1985, 8 p.m.


Conditional Tags

Conditional tags allow you to conditionally include content in your campaigns. They give you powerful controls for personalizing your campaigns to each individual recipient and, if your campaign is an autoresponder triggered by what someone did, include information from that event. 

If-Else Blocks

"If-else" blocks allow you to control the content someone receives in an email based on a variable's value.

{% if person|lookup:'Interested in Dogs?' %}   

 Like dogs? Check out some great toys for your canine.  

{% else %}   

 Like cats? Check out some great toys for your feline.  

{% endif %}. 

In the example above, a person will be shown the first line of text if "Interested in Dogs": is true. If it isn't, they will be shown the line about cats. 

For Blocks

"For" blocks allow you to iterate over each item in a variable that's storing a list and render them individually.

{% for item in event.shopping_cart_items %} {{ item.name }}  × {{ item.quantity }}   {% endfor %} . Oversized Beach Blanket × 1Beach Chairs × 430 SPF Sunscreen × 220" Plastic Cooler × 1  


Basic Filters 

Filters allow you to modify how a variable is displayed. You'll notice they go directly after a variable. The variable and filter should be separated with a pipe character (|). Some filters take an argument. In these cases, after the filter use a colon (:) and then surround the argument with quotes.

Below are some common use cases and examples:

Lookup Filter {{ person|lookup:'Birthday' }}
Default Filter  {{ first_name|default:'friend' }}


This filter is used to specify a default in case a variable doesn't have a value. This is useful as a placeholder when you don't always have the information you want to use, e.g. someone's name. (Django)

Split Filter {{ product_type|split:' - '|lookup:0 }}                 


The split filter is used to split the variable you pass into a zero-indexed array based on the value you pass. For example, if you have a property with the value "Shirt - Large," large is the "lookup:0" value.

To format the date of an event variable add the filter: |slice:':10'|datetime_from_string|date:'d/m/Y' 

 On {{ event.extra.customer.updated_at|slice:':10'|datetime_from_string|date:'d/m/Y' }}
On 10/12/2015 

When using this date filter, you can change the format of how the date appears by referring to this guide.

floatadd

You can use this filter like the "add" filter, but instead of forcing everything to integers, it'll keep the decimal points and do the addition.

floatformat

In addition to all the filters above, Klaviyo supports most of the filters used by the Django template language. As an example of one of the filters included, the floatformat filter allows you to format numbers.

You earned {{ event|lookup:'Points Earned'|floatformat:1 }}  points. You earned 25.5 points.

Learn more

View all the built-in tags and filters available with Django.

Klaviyo Special Properties

If you are using our API to set properties on a user's profile, Klaviyo has a few built-in fields you can set with the Identify API, our Profiles API, or via a CSV import:

  • $id: your unique identifier for a person
  • $email: email address
  • $first_name: first name
  • $last_name: last name
  • $phone_number: phone number
  • $title: title at business or organization
  • $organization: business or organization
  • $city: city
  • $region: region or state
  • $country: country
  • $zip: postal code
Was this article helpful?
0 out of 0 found this helpful