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:
- The "object" part is the object which you are checking the condition against. This is the only part of the condition that is required.
- 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.
- The "operand" is the final argument for the condition. For comparison operators (==, !=, < >, <=, >=, contains, etc...) the operand is required and should be a second object to check against. For type operators (is_valid, is_boolean, is_date, etc...) the operand is not required. Some of the type operators (is_boolean, is_date, is_list, and is_number) may return a different result if you supply true as the operand.
Operators
- ==
- 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.
- is_boolean
- Returns true if the object is true or false. To check if the object is a string that can be safely converted to true or false use {% if subject is_boolean true %}.
- is_date
- Returns true if the object is a date object. To check if the object is a string that can be safely converted to a date object use {% if subject is_date true %}.
- is_list
- Returns true if the object is a list. Note that while it is possible to enumerate through the characters in a string, a string will return false for is_list (eg: {% if "string" is_list %} will be false).
- is_number
- Returns true if the object is a number. To check if the object is a string that can be safely converted to a number use {% if subject is_number true %}.
- is_object
- Returns true if the object is not a list and is not a "simple type" (ie: if the object has its own properties). To return true if the object is a list use {% if subject is_object true %}. Note that collection objects such as {{ articles }} are actually objects although they can also be treated as a list.
- is_simple_type
- Returns true if the object is a string, date, number, boolean.
- is_string
- Returns true if the object is a string.
- 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.
Examples
Show Sidebar? {{ page.show_sidebar }}
{% if page.show_sidebar.checked %}
{% include "Sidebar" %}
{% endif %}
{% 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 %}
{% 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 %}