{% if %}

Use this tag to only execute a block of code if a certain condition is met.

Optionally, inside the if block, use one or more {% elsif %} tags to add more blocks and conditions to the if tag.

Optionally, at the end of the if block, use an {% else %} tag to execute a block of code if none of the conditions inside the if tag and corresponding elseif tags were met.

To close the if block, use {% endif %}

Alternatively, if you only want to execute a block of code if a certain condition is NOT met, use the unless tag.

Syntax

{% if condition %}...{% elsif condition %}...{% else %}...{% endif %}

You can use the and and or operators to include more than one condition. These can be chained together to create complex conditionals.

If you use multiple and and or operators, note that and operators will be evaluated first, then or operators. You cannot use parentheses to simulate an order of operations and control the order of operator evaluation. Parentheses are invalid characters and will prevent your tags from working.

The if tag creates a new child scope.

Conditions

Each condition must have between one and three parts:

  1. The "object" part is the object which you are checking the condition against. This is the only part of the condition that is required.
  2. The "operator" describes the type of conditional check you would like to perform. If there is no operator, the condition will return true if the left object exists and is non-null.
  3. The "operand" is what you are using to check the object against. You cannot include an operand without an operator, and almost all operators require an operand in order to work properly (the primary exception being the is_valid operator which does not use an operand).

Operators

is_valid
Returns false if the value is null, empty, false, an empty guid, or is a Marketpath CMS object with is_valid == false. Otherwise returns true.
==
Returns true if the object and operand are equal. Note that this does not always work as expected with objects - for Marketpath CMS objects use object.guid == operand.guid instead.
!= or <>
Returns true if the object and operand are NOT equal. Note that this does not always work as expected with objects - for Marketpath CMS objects use object.guid != operand.guid instead (or <>).
<
Returns true if the object is less than the operand. If the object and operand are not the same object type (eg: number and string) then the operand will be converted to the object type of the object before the comparison.
>
Returns true if the object is greater than the operand. If the object and operand are not the same object type (eg: number and string) then the operand will be converted to the object type of the object before the comparison.
<=
Returns true if the object is less than or equal to the operand. If the object and operand are not the same object type (eg: number and string) then the operand will be converted to the object type of the object before the comparison.
>=
Returns true if the object is greater than or equal to the operand. If the object and operand are not the same object type (eg: number and string) then the operand will be converted to the object type of the object before the comparison.
contains
If the object is a string, will return true if the string contains the operand as a string. If the object is a list, will return true if the list contains the operand. This is most useful for strings and for lists of strings.
starts_with
If the object is a string, will return true if the string starts with the operand as a string. If the object is a list, will return true if the first item in the list is the operand.
ends_with
If the object is a string, will return true if the string ends with the operand as a string. If the object is a list, will return true if the last item in the list is the operand.
has_key
For lists of key value pairs, returns true if the object has the operand key.
has_value
For lists of key value pairs, returns true if the object has the operand value.

Examples

{% if page.show_sidebar.checked %}
    {% include "sidebar" %}
{% endif %}
{% if random_object is_valid %}
    {% include "display_random_object" object:random_object %}
{% endif %}
{% unless request.query_params contains "show_sidebar=false" %}
    {% if request.query_params has_key "show_sidebar" %}
        {% include "_custom_sidebar" type:request.query_params.show_sidebar %}
    {% else %}
        {% include "_default_sidebar" type:request.query_params.show_sidebar %}
    {% endif %}
{% endunless %}
{% if entity.object_type == 'tag' %}
    I am a tag
{% elsif entity.object_type == 'author' %}
    I am an author
{% elsif entity.object_type == 'folder' %}
    I am a folder
{% else %}
    What am I?
{% endif %}
{% if page.show_weight_notice.checked or entity.weight.value > 20000 and request.cookie['Is Local'] == 'true' %}
    You're buying a heavy item. Choose local pick-up during checkout to avoid paying high shipping costs.
{% endif %}

Developer Overview

Liquid Markup