Control how your data is presented
Klaviyo gives you immense flexibility to display customer data in just about any message. But have you ever wondered how this actually works? The answer is Django.
Django is a templating language that allows you to embed dynamic content in your templates. You can use Django to display and format customer and event data, use conditional logic, and write for loops to iterate over your data. This guide will help you use Django variables and tags to completely customize your email templates.
In this lesson, you will learn how to:
- Identify and implement variables using Django.
- Write if statements and for loops in Django.
- Add filters to customize variables.
Note: Before reviewing this guide, you should have a basic understanding of programming logic. For a refresher, reference our quick guide on how to identify programming logic concepts.
Display customer data using Django variables and tags
Watch the video below for a live walkthrough of using Django to display and filter variables, write if statements, and implement for loops.
Use proper Django syntax
Django is a written language with symbols that have specific meanings. That means, just like English or French, there is a specific syntax (or formatting) that you must use in order for the Django commands to be executed correctly. Read more about the correct Django syntax for the following variables and tags.
Note that:
- Event and profile properties are called variables and use double curly braces.
{{ variable }}
- If statements and for loops are called tags and use curly brace percent sign.
{% tag %}
Variables are often included inside of tags. You do not need double curly braces for variables inside tags.
For all of the instructions below, "special characters" refers to anything that is not a number or a letter.
Display profile properties
If the profile property you wish to display is only 1 word without any special characters, display this property using the syntax:
{{ person.VARIABLENAME }}
If the property has special characters or is multiple words, use:
{{ person|lookup:'VARIABLENAME' }}
The following example shows a customer's environment and primary skin concern:
Display event properties
In metric-triggered and price drop flows, you can use Django variables to display properties of the event that triggered the flow. You cannot display event properties in campaigns or in any other type of flow.
Similar to profile properties, you can access 1-word properties with no special characters using:
{{ event.VARIABLENAME }}
If the name of the property has multiple words or any special characters, display it using:
{{ event|lookup:'VARIABLENAME' }}
You can also access nested properties by chaining periods or lookup filters. Check out this example event payload:
The variable Items contains all of the items in an order. These items are numbered, and each item contains some additional information. The product name Dry skin serum is said to be "nested" within item 0, which is nested within the Items variable.
You can display this product's name and quantity using:
Name: {{ event.Items.0|lookup:'product name' }}
Quantity: {{ events.Items.0.quantity }}
Each period represents a nested layer.
Conditional logic
Conditional logic (i.e., if/else statements) can be used in both email and SMS messages. If/else statements are Django tags, and the format looks like:
{% if person.environment == "humid" %} Humidity got your skin sweating? {% elif person.environment == "dry" %} Intensive moisturizers for dry air {% else %} Clean skincare for all skin types {% endif %}
In this example, person.environment == "humid" and person.environment =="dry" are booleans and can be evaluated as "true" or "false" depending on whether the profile is in a humid or dry environment.
You can use and and or within if statements to create more complex conditions:
This example SMS shows different content for:
- Customers concerned about wrinkles or fine lines.
- Customers with fair skin who are also concerned about sun damage.
- Everyone else.
Follow along with this conditional logic guide to write the conditions in your if/else statements.
For loops
You can use a for loop to go through any variable that contains multiple elements. The syntax looks like:
{% for item in event.Items %}{{ item }}
{% endfor %}
In the above syntax, the word item can be anything you would like: this is how you will refer to each item in the variable you are iterating through. Event.items will be replaced with the name of this variable.
For example, this profile has several skin concerns within the "skin concerns" profile property:
You can write a for loop to iterate over the skin concerns property and display each one in the template.
You can also further customize your formatting by combining if statements with for loops. The following example includes all skin concerns in one sentence:
New products designed specifically for {% for concern in person|lookup:"skin concerns" %}{% if not forloop.last %}{{ concern }}, {% else %}and {{ concern }}{% endif %}{% endfor %} skin.
This code iterates through the values in the skin concerns property and checks if each one is the last value in the list by using forloop.last, which is True only for the last item in the loop. If it is not the last item, the skin concern is followed by a comma and a space, while the last item is preceded by the word "and." This allows you to format the skin concerns property in a neat and natural way.
Customize your data with filters and tags
Django filters allow you to manipulate the format and content of your variables so they appear exactly as you want. Filters are applied after the pipe symbol (i.e., |) immediately after the variable name.
One filter that you have already seen is the lookup filter. Let's go back to the example of displaying a profile's "primary skin concern" property:
{{ person|lookup:"primary skin concern" }}
This filter takes the variable, person, and looks up and displays the value in quotes, primary skin concern.
Another example of a filter is the divide filter. Let's say your event data contains prices without decimal points. In other words, the price of a product worth $21.00 comes through as 2100. In order for this to display properly, we need to divide 2100 by 100.
The syntax for this is:
{{ event.price|divide:100 }}
The divide filter automatically divides the price by the number specified after the filter.
Then, you can use the currency_format tag:
{% currency_format event.price|divide:100 %}
This will automatically format your price in the currency specified in your template.
You can also use multiple filters on the same variable. Consider the following code:
{{ person.organization|title|default:"your organization" }}
The title filter first capitalizes the organization profile property, then applies the default filter to specify a value to display if organization is not set.
More Django filters are available in our glossary of variable filters.