How to conditionally show blocks in an email

Overview

Avoid creating multiple emails with slight differences by using templating syntax to show blocks conditionally based on contact attributes! Read on to learn how to show or hide email blocks conditionally.

In this guide

🚦 Before getting started

Ensure you've collected data for your custom fields. Conditionally showing or hiding content based on custom fields requires enough contacts to have specified data in their profiles for that custom field.


Show email blocks based on custom field data

You can conditionally show email blocks with just two snippets of templating syntax:

  1. Opening if statement - Tells the email builder to check if the condition in the if statement is true (the contact's profile contains the specified data for the specified custom field).

    If the if statement is true, the blocks between the if statement and the endif tag will be displayed. If the if statement is false (the contact's profile does not contain the specified data for the specified custom field), the content between the if statement and the endif tag will not be displayed.
  2. endif tag - Tells the email builder the if statement has ended

The example in the screenshot below shows a simple if statement in action. Contacts whose profiles have the favorite color field populated with red will see all blocks between the text blocks containing templating syntax.

Each separate email builder block is surrounded by a gray box and identified with a number in the screenshot below:

  1. A text block containing the opening if statement {% if contact.custom('Favorite Color') == "red" %}
  2. A text block containing copy intended for recipients whose Favorite Color custom field is set to red
  3. An image block containing an image intended for recipients whose Favorite Primary Color custom field is set to red
  4. A text block containing the endif tag {%-endif-%}
  5. A text block containing copy intended for all recipients
  6. An image block containing an image intended for all recipients

Contacts whose Favorite Color custom field is set to red will see the following in their inbox:

Contacts whose Favorite Color custom field is NOT set to red (i.e., it is null or set to another color) will see the following in their inbox:

Did this answer your question? Thanks for your feedback! There was a problem submitting your feedback. Please try again!