Skip to content

If Else

harrydeluxe edited this page May 13, 2012 · 1 revision

if / else should be well known from any imaginable programming language. Liquid allows you to write simple expressions in the if or unless (and optionally, elsif and else) clause:

{% if user %}
  Hello {{ user.name }}
{% endif %}

{% if user.name == 'tobi' %}
  Hello tobi
{% elsif user.name == 'bob' %}
  Hello bob
{% endif %}

{% if user.name == 'tobi' or user.name == 'bob' %}
  Hello tobi or bob
{% endif %}

{% if user.name == 'bob' and user.age > 45 %}
  Hello old bob 
{% endif %}

{% if user.name != 'tobi' %} 
  Hello non-tobi
{% endif %}

# Same as above
{% unless user.name == 'tobi' %}
  Hello non-tobi
{% endunless %}

# Check if the user has a credit card
{% if user.creditcard != null %}
   poor sob
{% endif %}

# Same as above 
{% if user.creditcard %}
   poor sob
{% endif %}

# Check for an empty array
{% if user.payments == empty %}
   you never paid ! 
{% endif %}

{% if user.age > 18 %}
   Login here
{% else %}
   Sorry, you are too young
{% endif %}

# array = 1,2,3
{% if array contains 2 %} 
   array includes 2
{% endif %}

# string = 'hello world' 
{% if string contains 'hello' %} 
   string includes 'hello'
{% endif %}