Tutorial

Writing If/Endif/Elif/Else Statements

  • 27 June 2022
  • 4 replies
  • 1137 views
Writing If/Endif/Elif/Else Statements
Userlevel 3
Badge +1

Similar to using the show/hide function, using If/Endif statements can be used to show certain copy if the viewer meets the if/endif statement requirements, based on their saved profile properties. IE: You have a form on your website that asks if customers prefer cats or dogs as a pet. The customer fills in the form with their email address, stating they like dogs.  A different customer fills in the form saying they like cats.  In an email template, you can write an if/endif statement that directs customers who filled in ‘dogs’ to see one statement, and a different statement for customers who filled in ‘cats’ to see a different statement. 

The if/endif statement for this would look like the following:

 

{% if person|lookup:'Interested in Dogs?' and not person|lookup:'Interested in Cats?' %}  

 Like dogs? Check out some great toys for your canine.

{% elif person|lookup:'Interested in Cats?' and not person|lookup:'Interested in Dogs?' %}  

 Like cats? Check out some great toys for your feline.

{% else %} Check out some great toys for your pet!

{% endif %}

When writing an if/endif statement based on a saved profile property, the statement will always include person|lookup: ‘Profile Property name here’.

This is because we’re having the if/endif function look on the viewer’s profile for this property, and display the following condition, if they have that property, with the statement you added after (IE: the function is looking for the profile property ‘interested in dogs’ and if yes, showing ‘Like dogs?...’). 

‘If statements’ should follow this general structure. The conditions in ALL CAPS are placeholders.

{% if CONDITION_1 %}

Content to show if CONDITION_1 is true.

{% elif CONDITION_2 %}  

If CONDITION_1 is not true, but CONDITION_2 is true, show this content instead.

"Elif" statements are optional.

{% else %}

If none of the above conditions are true, show this content.

"Else" statement is optional.

{% endif %}

The tags {% elif %} and {% else %}  are completely optional. Any amount of {% elif %} tags can be used to define alternate conditions and only one {% else %} should be used to define default content if desired. However, {% endif %} is absolutely necessary to end the if statements.

 

More information on this can be found here.

More information on profile properties can be found here.


4 replies

Badge +1

Is there an option to write an IF/THEN statement for a price drop event?

Let’s say I am building a re-order reminder flow. I am including a dynamic table showcasing products that a customer purchased last time. These are the parameters I have currently:

{{ item.product.title }}

Quantity: {{ item.quantity }}

Total: ${{ item.price }}

Can I add an IF statement to include a sales price dynamic parameter IF there is a deal on that particular product?

 

Thanks!

Userlevel 7
Badge +58

Hey @Anna Orishchenko 

Thanks for sharing! You can us If/Else statement to show discount price. It should look something like this: 

{% if item.sale_price != item.product.price %}${{ item.sale_price }} <s>{{ item.product.price }}</s>{% else %}{{ item.product.price }} {% endif %}

Hope this helps!

Badge +1

This is neat, thanks! I realize now, though, that I can’t add this statement into a Product block, right? I can only use it to built a product from scratch - for example, in the Dynamic Table Block?

Badge +1

Hmm, actually, I just tried this, and it doesn’t work:

 

That’s how I inserted the code.
That’s what I see in the preview

 

Reply