Conditionals

IF - ELSEIF

IF is one of the simplest decision making logic blocks. It can be used either to check if a variable exists and do something if it is (or it is not) or check if a statement is true or false.

In this example let's check if the logged in user's firstName exists and if so use it. Otherwise, if the site visitor is not logged in or if the site visitor has not filled in his or her's firstname we will only show "Hello,":

  {% if globals.user.firstName %}
    <p>Hello {{globals.user.firstName}},</p>
  {% else %}
    <p>Hello, </p>
  {% endif %}

You can also use multiple variables in an IF block, for example let's assume we have a variable that stores the user's age in years and another one that stores the days since the user's last purchase:

  {% if myUserData.age > 30 and myUserData.lastOrder < 20 %}
    <p>Show special offer for users above 30</p>
  {% endif %}

You can optionally use elseif or else:

  {% if myUserData.type == "partner" %}
    <p><a href="/partner-login.html"></p>
    {% elseif myUserData.type == "regular" %}
    <p><a href="/login.html"></p>    
  {% endif %}

or:

  {% if myUserData.type == "partner" %}
    <p><a href="/partner-login.html"></p>
  {% else %}
    {% if myUserData.type == "regular" %}
    <p><a href="/login.html"></p>    
  {% endif %}

CASE

Simmilar to multiple nested IF statements, CASE allows you to take different actions based on the value of an evalueated output tag or variable.

{% case item.country %}
  {% when 'DE' %}
    Willkommen
  {% when 'ES' %}
    Bienvenido
  {% when 'GB' %}
    Welcome
  {% else %}
    Bine ai venit
{% endcase %}

In the example above item.country is tested against the known values DE, ES and EN and if it has a different value "Bine ati venit" is rendered.

You can also use the OR or AND operators with your checks checks:

{% case item.country %}
  {% when 'DE' or 'AT'%}
    Willkommen
  {% when 'ES' or 'TD' %}
    Bienvenido
  {% when 'GB' or 'US' or 'AU'%}
    Welcome
  {% else %}
    Bine ai venit
{% endcase %}

UNLESS

UNLESS is the opposite if IF, the condition is executed if the condition is false. For example:

  {% unless myUserData.type == "partner" or myUserData.type == "regular" %}
    <p><a href="/login.html"</p>    
  {% endunless %}