Conditional logic reference for templates

Estimated 6 minute read
|
Updated Oct 28, 2024, 6:55 PM EST
You will learn

You will learn

Learn about all the available conditions you can use to dynamically display a block, section, or custom-coded piece of content in an email template only to certain recipients. To learn how to use these conditions in your templates, head to How to show or hide template blocks based on dynamic variables

Learn how to use conditional logic:

Where you can use conditions

Where you can use conditions

You can use conditions: 

Tips for success

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 recipients for your email. 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.

Once you’ve built the message and applied your conditions, preview the email using a variety of profiles to confirm that the message displays as you intended for all scenarios. 

Condition structures

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 specific 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. 

The chart below contains a complete list of possible structures a show/hide condition can follow. 

Sample conditionShow the block if...Acceptable data types

person|lookup:'Favorite Color'

The Favorite Color property is set (has any value) and is not the boolean False

Any

not person|lookup:'Favorite Color'

The Favorite Color property is not set (does not exist on the profile, or is empty), or is the boolean value False

Any

person|lookup:'Favorite Color' == 'green'

The Favorite Color property has the value green

Text, Number

person|lookup:'Favorite Color' != 'green'

The Favorite Color property does not have the value green

Text, Number

person|lookup:'Age' > 20

The Age property contains a number greater than 20

Number

person|lookup:'Age' >= 20

The Age property contains a number greater than or equal to 20

Number

person|lookup:'Age' < 20

The Age property contains a number less than 20

Number

person|lookup:'Age' <= 20

The Age property contains a number less than or equal to 20

Number

'green' in person|lookup:'Favorite Colors'

The property Favorite Colors contains a list, and green is one of the list items, OR

The property Favorite Colors contains text, and green exists anywhere in the text

List, Text

not 'green' in person|lookup:'Favorite Colors'

The property Favorite Colors contains a list, and green is not one of the list items, OR

The property Favorite Colors contains text, and green does not exist anywhere in the text

List, Text

Conditions for booleans

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 conditionShow the block if...

person|lookup:'VIP' == 1

The VIP property is set to the boolean value true

person|lookup:'VIP' == 0

The VIP property is set to the boolean value false

Conditions for booleans stored as text

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 conditionShow the block if...

person|lookup:'VIP' == 1 or person|lookup:'VIP' == 'true' or person|lookup:'VIP' == 'True'

The VIP property is set to the boolean value true or the strings true or True

person|lookup:'VIP' == 0 or person|lookup:'VIP' == 'false' or person|lookup:'VIP' == 'False'

The VIP property is set to the boolean value false or the strings false or False

Build complex conditions

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 a series of conditions with AND or 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

Conditional statements and the inline text editor

Conditional statements and the inline text editor

When you add certain conditional statements to a text block, they may disappear from the inline text editor. The code is still present; it is just hidden. To view and edit conditional statements, open the text block's Source code field. 

The following tags are only visible in a text block's Source code field: 

  • {% for ... %}
  • {% endfor %}
  • {% if ... %}
  • {% elif ... %}
  • {% else %}
  • {% endif %}
  • {% with ... %}
  • {% endwith %}
Additional resources

Additional resources

Was this article helpful?
Use this form only for article feedback. Learn how to contact support.

Explore more from Klaviyo

Community
Connect with peers, partners, and Klaviyo experts to find inspiration, share insights, and get answers to all of your questions.
Live training
Join a live session with Klaviyo experts to learn about best practices, how to set up key features, and more.
Support

Access support through your account.

Email support (free trial and paid accounts) Available 24/7

Chat/virtual assistance
Availability varies by location and plan type