Conditional logic reference for templates
You will learn
Learn about all the available conditions you can use to dynamically display a block, section, or custom-coded piece of content only to certain recipients. You can use these same conditions for each show/hide logic variant. To learn how to use these conditions in your templates, head to our article on how to show or hide template blocks based on dynamic variables.
Learn how to use conditional logic:
- Where you can use conditions
- Tips for success
- Condition structures
- Build complex conditions
- Use conditions in a converted HTML template
Where you can use conditions
You can use conditions in:
- Emails
- Create show/hide logic to dynamically display blocks or sections only to certain people.
- If you prefer not to write code, use the show/hide logic builder instead.
- Create show/hide logic variants to show a different version of a block or section to each audience. Each logic variant has its own condition, which you can build with the logic builder or write as custom code.
- Write if/else conditionals to create custom Django statements.
- Create show/hide logic to dynamically display blocks or sections only to certain people.
- Customer Hub
- Write if/else conditionals to create custom Django statements in content blocks
Tips for success
When building out show/hide conditions, pay attention to the details. Show/hide conditions are case-sensitive, and spelling must exactly match your profile or event data.
Also, make sure to consider all possible viewers. For example, if you show a specific block only to residents of a certain state, cover all possible spellings of that state name (e.g., Massachusetts, massachusetts, mass, MA). Also take into consideration profiles who may not have the property set at all.
If you use logic variants, add a fallback so that recipients who don’t match any variant, including those without the property, still see content.
Once you’ve built the message and applied your conditions, preview using a variety of profiles to confirm that the message displays as you intended for all scenarios.
Condition structures
Conditions should include 1-3 elements, depending on your goal for the block and the data you’re using. The condition must at least include a variable (e.g., person|lookup:'Favorite Color'). It may also include a comparison function, like == (equals) or > (greater than) and a value, which specifies a property value to look for. Additionally, certain conditions begin with not, if you’d like the block to only appear for profiles that do not meet a condition.
For custom-coded show/hide logic, enter the expression by itself, without {{ }} or {% if ... %} tags. Add spaces on both sides of comparison operators. Use straight quotes around text values, such as 'green', and leave numbers unquoted, such as 20.
The chart below contains a complete list of possible structures a show/hide condition can follow. The same structures apply to the condition for each logic variant.
Sample condition | Show the block if... | Acceptable data types |
person|lookup:'Favorite Color' | The | Any |
not person|lookup:'Favorite Color' | The | Any |
person|lookup:'Favorite Color' == 'green' | The | Text, Number |
person|lookup:'Favorite Color' != 'green' | The | Text, Number |
person|lookup:'Age' > 20 | The | Number |
person|lookup:'Age' >= 20 | The | Number |
person|lookup:'Age' < 20 | The | Number |
person|lookup:'Age' <= 20 | The | Number |
'green' in person|lookup:'Favorite Colors' | The property The property | List, Text |
not 'green' in person|lookup:'Favorite Colors' | The property The property | List, Text |
Conditions for booleans
If you are referencing data stored as a boolean, you’ll need to use 1 and 0 rather than “true” and “false” in your show/hide condition definition. Do not surround the 1 or 0 in quotes. Use the sample conditions below as a template.
Sample condition | Show the block if... |
person|lookup:'VIP' == 1 | The |
person|lookup:'VIP' == 0 | The |
Conditions for booleans stored as text
If your true/false data is stored as text, not as a boolean, use the sample conditions for text properties above. If you aren’t sure, or if you are referencing a property that contains both booleans and text, you can use these structures to cover all scenarios. Include all spellings and capitalizations that are present in your data.
Sample condition | Show the block if... |
person|lookup:'VIP' == 1 or person|lookup:'VIP' == 'true' or person|lookup:'VIP' == 'True' | The |
person|lookup:'VIP' == 0 or person|lookup:'VIP' == 'false' or person|lookup:'VIP' == 'False' | The |
Build complex conditions
If you would like your block to display to people who meet multiple criteria, or if you have a complex use case, you can use multiple show/hide conditions for one block. To do so, connect the conditions with lowercase and or or. Use and when both conditions must be true, and or when either can be true. When you combine them, and is evaluated before or. For example, if you want to display a block to anyone in Massachusetts, but Massachusetts is spelled differently on some profiles, you can use a condition like this:
person.location.region == 'Massachusetts' or person.location.region == 'massachusetts' or person.location.region == 'mass' or person.location.region == 'MA'
If you only want to show a block to people whose favorite color is green, and who are also VIPs, you can use a condition like this:
person|lookup:'Favorite Color' == 'green' and person|lookup:'VIP' == 1
Conditions across logic variants
When a block or section has show/hide logic variants, each variant has its own condition. Variants are checked from top to bottom, and each recipient sees only the first variant they match. Because of this, a later variant’s condition doesn’t need to exclude audiences that an earlier variant already covers.
For example, a block could have these 2 variants:
- Variant 1: person|lookup:'VIP' == 1
- Variant 2: person|lookup:'Favorite Color' == 'green'
A VIP whose favorite color is green sees only Variant 1. To show them Variant 2 instead, drag Variant 2 above Variant 1 or make Variant 1’s condition more specific.
To show content to everyone who doesn’t match a variant, add a fallback instead of writing a catch-all condition such as person|lookup:'Favorite Color' != 'green' and person|lookup:'VIP' != 1. The fallback doesn’t have a condition and also covers recipients who don’t have the property set.
Conditional statements and the inline text editor
Supported Django statements in text blocks appear as logic blocks in the inline text editor. Their tags remain part of the template. To edit a condition:
- Double-click the text block.
- If This block uses unsupported formatting appears, review the formatting options. Select Edit in updated text block to continue with adjusted styling. To keep the existing formatting, select Convert to HTML block and edit the code in the Content tab instead of following the remaining steps.
- Click the logic block’s Edit pencil to open Edit logic block.
- Update the condition and click Save. Click outside the text block to finish editing.
You can also edit Django statements directly in an HTML block’s Content tab.
The following tags are represented in logic blocks:
- {% for ... %}
- {% endfor %}
- {% if ... %}
- {% elif ... %}
- {% else %}
- {% endif %}
- {% with ... %}
- {% endwith %}
Use conditions in a converted HTML template
To use the drag-and-drop editor with a saved HTML template, create a converted copy. You need permission to edit templates. The original HTML template stays unchanged.
- Navigate to Content > Templates and select Email: saved.
- Open the HTML template’s 3-dot menu and select Clone to drag and drop.
- Select Converted templates. When the copy’s status is Ready to use, click its name to open it in the editor.
Review the copy’s conditional content and make any needed edits. Click Preview and check profiles that meet your conditions, profiles that do not, and profiles without the referenced property before using the template.
Additional resources
- How to show or hide template blocks and sections based on dynamic variables
Learn how to show a block or section to certain recipients with show/hide logic, and show different content to multiple audiences with show/hide logic variants.
- Message personalization reference
Learn how to use Klaviyo’s personalization variables, or tags, to customize the experience for your customers.
- Understanding profiles in Klaviyo
Learn about profiles, including how to find them and the data available on the profile page. Profiles are used to keep track of all the information you have about your customers and prospects.
- Glossary of variable filters