---
title: "How to add conditional logic to email templates using the logic builder"
canonical_url: "https://help.klaviyo.com/hc/en-us/articles/55241564267547"
markdown_url: "https://help.klaviyo.com/hc/en-us/articles/55241564267547.md"
locale: "en-us"
category: "Content"
section: "Getting started with templates"
updated: "2026-10-27"
---

# How to add conditional logic to email templates using the logic builder

**You will learn**

Learn how to use the logic builder to add dynamic conditional logic—like if/else statements and for loops—directly to text blocks in your email templates. This allows you to display personalized content to different recipients based on their profile or custom properties, without writing code.

## **Before you begin**

The logic builder is available in:

- **Drag and drop email editor** — text blocks, table blocks, and split blocks

- **Text only email editor** — via a modal instead of a side panel



To select a custom property from the dropdown, first add it to at least 1 profile in your account. Profile and custom properties can be added through sign-up forms, CSV uploads, ecommerce integrations, and other list growth tools.

**Note:** The property dropdown includes profile and custom properties, but does not list event data. To write a condition using event data, you can use **Convert to code** and write Django tags directly. For loops can use an event list directly in **Variable to loop over**. Use a flow triggered by the event whose data you want to reference.

## **Add an if/else statement**

Use an if/else statement to display different content to recipients based on a profile or custom property. For example, you might show a VIP discount block only to customers with more than 500 loyalty points, and a sign-up incentive to everyone else.

### **In the drag and drop editor**

1. Double-click into a **text block**, **table block**, or **split block** to enter editing mode.

2. Click the **Insert** button in the text formatting toolbar.

3. Select **If/else condition** from the dropdown menu.

   - A side panel will open on the left, and the canvas will shift to the right.

4. Configure your **if** statement:

   - Use the first dropdown to choose a **profile or custom property** to evaluate.

   - Use the second dropdown to select an **operator** (e.g., equals, greater than, is set).

   - Enter or select a **value** to compare against, depending on the operator chosen.

5. Optionally, click **Add condition** to add additional criteria to the if statement.

   - Use **AND** or **OR** to connect conditions. AND is selected by default. Conditions connected by AND must all match; conditions connected by OR provide alternatives.

   - Click the **Copy condition** button on a condition to duplicate it as a new AND condition.

6. To add an **else if** branch, click **Add else if** and configure the conditions the same way. Recipients see the content for the first branch they match. An else if branch applies only when the if statement and all earlier else if branches do not match.

7. The fallback checkbox is selected by default: **Include else content for all profiles who do not match any conditions.** Keep it selected to show content to recipients who match no conditions, or clear it to remove the else branch.

8. Edit the content in the canvas for each condition. Each condition appears as a **blue box** while being configured.

9. When finished, click **Save** or the **back arrow** to close the panel and save your logic.



**Tip:** To remove the selected logic, click **Remove logic** in the side panel. The content remains in the email, so review it after removing the conditions.

**Note:** Click **Delete condition** to remove a condition. Deleting the last condition in the initial if statement clears its values but keeps the statement. Deleting the last condition in an else if branch removes that branch. To remove the else branch, clear the fallback-content checkbox.

### **In the text only editor**

In the text only editor, select **Insert > If/else condition** from the text editing toolbar. Configure the conditions in the dialog using the options above, then click **Save**. Edit the content for each branch in the email after the dialog closes.

## **Add a for loop**

Use a for loop to iterate through a list of items and display repeated content for each one. For example, you might loop through a customer's recently viewed products to show each one in the email.

### **In the drag and drop editor**

1. Double-click into a **text block**, **table block**, or **split block**.

2. Click the **Insert** button in the toolbar.

3. Select **For loop** from the dropdown menu.

   - A side panel will open on the left.

4. In the **Variable to loop over** field, enter the list you want to loop through (e.g., event.items, if that is the list variable in your flow's trigger event). Use the exact variable name from your data.

5. In the **Item alias** field, enter a name to reference each individual item within the loop (e.g., item).

6. Edit the content in the canvas to define what will be displayed for each item. Use the item alias in personalization tags: for an alias of `item`, use `{{ item }}` for a simple list value, or a field such as `{{ item.name }}` if each item has a name property.

7. Click **Save** or the **back arrow** to close the panel and save your logic.



**Tip:** To remove the loop, click **Remove logic** in the side panel. The content remains, so remove or replace any tags that depend on the item alias.

## **Add a configurable tag**

Use a configurable tag to insert Django tags that aren't covered by the if/else or for loop builders—such as with statements or catalog tags.

1. Double-click into a **text block**, **table block**, or **split block**.

2. Click the **Insert** button in the toolbar.

3. Select **Configurable tag** from the dropdown menu.

   - A side panel will open on the left.

4. In the **Tag name** field, enter the tag that will open and close the block (e.g., with).

5. In **Tag arguments**, enter only the arguments for the tag. For example, for a `with` tag, enter `first_name as name`. Do not include the tag name or the opening and closing tag delimiters.

6. Edit the content in the canvas that belongs inside the tag. For the `with` example above, use `{{ name }}` where you want the recipient’s first name to appear.

7. Click **Save** or the **back arrow** to close the panel and save your logic.



## **Work with nested logic**

You can nest logic statements inside each other—for example, an if/else statement inside a for loop. This allows you to build highly targeted content sequences.

When logic is nested:

- **Vertical line icons** indicate the nesting level of each logic statement.

- Hovering over a **parent** statement highlights the full nested block.

- Hovering over a **child** statement highlights only that child.

- You can **collapse and expand** child statements independently from the parent.

- If more than 3 levels of logic are nested, a **+[number]** indicator appears to show the additional nesting depth.



## **Convert logic to code**

If you need more control over a condition or loop, you can convert it to Django code. For example, you can write event-based conditions or date comparisons using code.

**Warning:** After you save a conversion, you cannot switch that condition or loop back to the visual builder. You must continue editing it as code. Other conditions can still use the visual builder.

To convert:

1. Click **Edit** on the logic statement you want to update to open its side panel or, in the text only editor, its dialog.

   - For **if/else statements**, each individual segment (if, else if) has its own **Convert to code** button. Click the one for the segment you want to convert.

   - For **for loops**, click the single **Convert to code** button for the entire loop.

2. In the warning dialog, click **Convert** to proceed.

3. Edit the converted condition or loop arguments in the code editor, then click **Save** to apply your changes and close the side panel or dialog.



The saved conversion remains when you close the panel or dialog and reopen the template.

## **Differences in the text only editor**

The logic builder in the text only editor works the same as in the drag and drop editor, with one key difference: logic is configured in a **modal** rather than a side panel. All the same options for if/else, for loops, configurable tags, nesting, and converting to code are available.

After configuring logic in the text only editor, click **Save** to close the dialog, then edit the content in the email. Click **Edit** on a configured statement to reopen the dialog. When you finish editing the email, use **Save** in the email editor to save the template. If you see **Review and save** instead, review the content and save it.

## **Preview your email**

Before sending, preview the email with profiles that match each branch and a profile that matches none of the conditions. For event-based logic, also preview with the relevant event data. Check that the expected content appears and that loops display the correct items.

## **Additional resources**

- [Conditional logic reference](https://help.klaviyo.com/hc/en-us/articles/7655926841499.md)

- [How to show or hide template blocks and sections based on dynamic variables](https://help.klaviyo.com/hc/en-us/articles/7655965301531.md)

- [Message personalization reference](https://help.klaviyo.com/hc/en-us/articles/4408802648731.md)

- [About custom properties](https://help.klaviyo.com/hc/en-us/articles/115000250912.md)

- Course: [Apply programming logic concepts to build more personalized messages](https://academy.klaviyo.com/apply-programming-logic-concepts-to-build-more-personalized-messages/1857035)
