[Manual] Show or hide content in your emails using if statements

Brevo offers two methods for showing or hiding content in your emails: using the Content visibility feature in the drag-and-drop email editor or typing if statements manually.
This article covers the manual method. If you prefer the Content visibility feature, check our dedicated article Show or hide content in your emails (Content visibility).

Use an if statement to show or hide parts of your email based on each recipient’s data. This allows you to send a single email that dynamically adapts to different contacts, depending on specific conditions.

You can define if statement conditions based on different criteria:

  • Contact attributes
    Show or hide sections of your email depending on values stored in contact attributes. For example, you might show a VIP-only message to contacts with the Status attribute set as "VIP".
  • Event data
    Personalize content based on actions your contacts have (or haven’t) taken. For example, you could show a promotional block only to contacts who have never placed an order.
  • Data feeds
    Personalize content using real-time data from external feeds. For example, you can show personalized product recommendations, live pricing, or localized store information based on external data.
  • Product feeds
    Personalize content using using product information from your ecommerce website. For example, you can show your best seller products, or personalized product recommendations.

What is an if statement?

An if statement is a conditional rule that decides whether certain content is displayed based on a specified condition. In email templates, if statements allow you to dynamically show or hide content according to recipient data, making your emails more targeted and personalized.

For example, the emails below are sent from the same template but display different content according to recipient data:

show_hide_content_example1_en-us.png show_hide_content_example2_en-us.png

Here are some examples:

  • Personalized greetings
    Display "Hello Mr. X" for men, "Hello Mrs. Y" for women, or simply "Hello" if the name or gender is missing.
  • Targeted images or promotions
    Show an image of a model wearing the recipient’s preferred clothing size or feature a product in their favorite color.
  • Custom promotional messages
    Include special offers in transactional emails based on past purchases or other contact attributes.

Structure and syntax of an if statement

An if statement must follow specific structure and syntax rules:

  • The statement begins with an if tag, followed by a condition: {% if ... %}
  • Optionally, you can include:
    • Alternative conditions, which are evaluated in sequence. These begin with {% elif ... %}
    • A catch-all clause just before the end of the statement with {% else %}.
  • Every if statement must end with {% endif %}.

Common ways to build an if statement

💡 Good to know
Avoid using an if statement on variables containing float values, as they may not produce accurate results. However, you can apply a statement if the float value is passed as a string (enclosed in quotation marks, e.g., "3.14").

Here are some common ways to build an if statement and show or hide content in your emails based on specific conditions:

if

Checks whether a value is true or an array contains elements (i.e., is not empty).

Example 1

Syntax
{% if condition %}
Add the content to display if the value is true or an array contains elements
{% endif %}
Example input
{% if contact.active %}
Congrats! You achieved your goal this month.
{% endif %}
Example output
Value is true or array contains elements Value is false or array is empty

Congrats! You achieved your goal this month.

Content is hidden

Example 2

Syntax
{% if condition %}
{% for loop %}
Add the content to loop if the value is true or an array contains elements
{% endfor %}
{% endif %}
Example input
{% if params.tutors %}
The following tutors are available to help you:
{% for tutor in params.tutors %}
{{ tutor.name }}
{% endfor %}
{% endif %}
Example output
Value is true or array contains elements Value is false or array is empty

The following tutors are available to help you:

Abby Smith

James Doe

Peter Parker

Content is hidden

==

Checks whether an expression is true.

Example 1

Syntax
{% if condition == "value" %}
Add the content to display if the expression is true
{% endif %}
Example input
{% if coupon == "WELCOME" %}
Welcome to our list! Here’s your first coupon: WELCOME25
{% endif %}
Example output
Expression is true Expression is false

Welcome to our list! Here’s your first coupon: WELCOME25

Content is hidden

Example 2

Syntax
{% if condition == true %}
Add the content to display if the expression is true
{% endif %}
Example input
{% if contact.donor == true %}
Thank you for your support!
{% endif %}
Example output
Expression is true Expression is false

Thank you for your support!

Content is hidden

if, in

Checks whether a value (substring) is present within a string or if a variable is found within an array.

Example 1

Syntax
{% if "value" in condition %}
Add the content to display if a value is present within a string or
if a variable is found within an array

{% endif %}
Example input
{% if "recent" in customer.last_purchase_status %}
Thank you for your recent purchase! We hope you love your new items.
{% endif %}
Example output
Value is present within a string or variable is found within an array No value is present within a string or no variable is found within an array

Thank you for your recent purchase! We hope you love your new items.

Content is hidden

Example 2

Syntax
{% if "value" in condition %}
Add the content to display if a value is present within a string or
if a variable is found within an array

{% endif %}
Example input
{% if "fragile" in params.type %}
Please handle with care! One or more fragile items are in your order.
{% endif %}
Example output
Value is present within a string or variable is found within an array No value is present within a string or no variable is found within an array

Please handle with care! One or more fragile items are in your order.

Content is hidden

not

Checks whether a value is false.

Example 1

Syntax
{% if not condition %}
Add the content to display if a value is false
{% endif %}
Example input
{% if not user.subscribed %}
   You are not subscribed to our secret sale alerts. Sign up here.
{% endif %}
Example output
Value is false Value is true

You are not subscribed to our secret sale alerts. Sign up here.

Content is hidden

and / or

Checks multiple conditions.

Example 1

Syntax
{% if condition1 and condition2 %}
Add the content to display if all the conditions are true
{% endif %}
Example input
{% if temperature > 10 and temperature < 55 %}
Brr. It’s cold! Here’s a coupon for 20% off of any hot beverage, today only.
{% endif %}
Example output
All the conditions are true At least one of the conditions is not true

Brr. It’s cold! Here’s a coupon for 20% off of any hot beverage, today only.

Content is hidden

Example 2

Syntax
{% if condition1 == "value" and condition2 == "value" %}
Add the content to display if all the conditions are true
{% endif %}
Example input
{% if contact.lang == "FR" and contact.country == "Canada" %}
À l'occasion de la fête du Canada ce 1er juillet 2024, profitez de 10 %
de réduction supplémentaires dans nos enseignes canadiennes !
{% endif %}
Example output
All the conditions are true At least one of the conditions is not true

À l'occasion de la fête du Canada ce 1er juillet 2024, profitez de 10 % de réduction supplémentaires dans nos enseignes canadiennes !

Content is hidden

Example 3

Syntax
{% if condition1 == "value" or condition2 == "value" %}
Add the content to display if at least one of the conditions is true
{% endif %}
Example input
{% if contact.country == "United States" or contact.country == "Canada" %}
Thank you for shopping with us! Enjoy free shipping on orders over $50 in the
United States and Canada.
{% endif %}
Example output
At least one of the conditions is true None of the conditions are true

Thank you for shopping with us! Enjoy free shipping on orders over $50 in the United States and Canada.

Content is hidden

elif, else

Checks multiple branches.

Example 1

Syntax
{% if condition1 == "value" %}
Add the content to display if condition1 is true
{% elif condition2 == "value" %}
Add the content to display if condition2 is true
{% else %}
Add the content to display if none of the conditions are true
{% endif %}
Example input
Hello 
{% if contact.gender == "Male" %}
Mr. {{ contact.lastname }},
{% elif contact.gender == "Female" %}
Ms. {{ contact.lastname }},
{% else %}
there,
{% endif %}
Example output
Condition1 is true Condition2 is true None of the conditions are true

Hello Mr. Smith,

Hello Ms. Jones,

Hello there,

Example 2

Syntax
{% if condition1 == "value" %}
Add the content to display if condition1 is true
{% elif condition2 == "value" %}
Add the content to display if condition2 is true
{% else %}
Add the content to display if none of the conditions are true
{% endif %}
Example input
{% if "express" in params.shipping_method %}
Your order is being shipped via Express Delivery! Expect it to arrive
within 1-2 business days.
{% elif "standard" in params.shipping_method %}
Your order is on its way with Standard Shipping. It should arrive
within 3-5 business days.
{% else %}
Your order is being processed. We will update you with tracking details soon.
{% endif %}
Example output
Condition1 is true Condition2 is true None of the conditions are true

Your order is being shipped via Express Delivery! Expect it to arrive within 1-2 business days.

Your order is on its way with Standard Shipping. It should arrive within 3-5 business days.

Your order is being processed. We will update you with tracking details soon.

⏭️ What's next?

🤔 Have a question?

If you have a question, feel free to contact our support team by creating a ticket from your account. If you don't have an account yet, you can contact us here.

If you’re looking for help with a project using Brevo, we can match you with the right certified Brevo Agency partner.

💬 Was this article helpful?

12 out of 26 found this helpful