Tutorial

Writing If/Endif/Elif/Else Statements

  • 27 June 2022
  • 6 replies
  • 3754 views
Writing If/Endif/Elif/Else Statements
Userlevel 4
Badge +2

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.


6 replies

Badge +2

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 +60

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 +2

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 +2

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

 

Badge

Can I write and if then state with a CONTAINS clause? Pretty much if shipping contains EXPEDITED I want to show different copy than if it doesnt

Badge +1

It would be great if you could add some information here or maybe even better on the help article concerning when you need to use == and when = (in our case doesn’t seem to make a difference. Also booleans: not using ‘false’ but using 0.

Took a while and a lot of trial and error to figure this all out writing our if/else statement.

Our final if/else statement that seems to work:

<div>{% if event.HasFreeShipping = 0 and event.FreeShippingFrom = 25 %} <strong>🚚 Free Shipping from 25&euro;</strong></div>
<div>{% elif event.HasFreeShipping = 0 and event.FreeShippingFrom = 35 %} <strong>🚚 Free Shipping from 35&euro;</strong></div>
<div>{% elif event.HasFreeShipping = 0 and event.FreeShippingFrom = 50 %} <strong>🚚 Free Shipping from 50&euro;</strong></div>
<div>{% elif event.HasFreeShipping = 0 and event.FreeShippingFrom = 100 %} <strong>🚚 Free Shipping from 100&euro;</strong></div>
<div>{% elif event.HasFreeShipping = 0 and event.FreeShippingFrom = 130 %} <strong>🚚 Free Shipping from 130&euro;</strong></div>
{% else %}
<div><strong>🚚 Free Shipping</strong>: That's right, we're offering FREE shipping on your order!</div>
{% endif %}

(‘HasFreeShipping’ and ‘FreeShippingFrom’ in this example are two fields we sent along with a check-out event (custom integration)

Reply