Conditions¶
With conditions you can take control about what should be displayed in your template based on the contents of placeholders.
If¶
With “if” you can check for example if a placeholder contains something you expect.
{% if Brand == "LEGO" %}
It's LEGO, yeah!
{% endif %}
{% if OfferIsPrime %}
It's a prime product
{% endif %}
{% if OffersMainPriceAmount > 1000 %}
Expensive!
{% endif %}
Else¶
If you want to output a default string, if a condition was not fulfilled, you can use the “else” condition.
{% if CustomerReviewsAverageRating > 4 %}
<img src="img/reviews/better_than_4.jpg">
{% else %}
<img src="img/reviews/default.jpg">
{% endif %}
Elseif¶
If you want to check for multiple conditions, you can use the “elseif” condition.
{% if LargeImageURL is not empty %}
<img src="{{ LargeImageURL }}">
{% elseif MediumImageURL is not empty %}
<img src="{{ MediumImageURL }}">
{% elseif SmallImageURL is not empty %}
<img src="{{ SmallImageURL }}">
{% else %}
<img src="img/no_image.gif">
{% endif %}
and¶
You can combine multiple conditions with the and
operator.
{% if Brand == "LEGO" and OfferIsPrime == true %}
It's LEGO and prime
{% else %}
It's something else
{% endif %}
or¶
You can combine multiple conditions with the or
operator.
{% if Brand == "LEGO" or Brand == "PLAYMOBIL" %}
It's something you want to play with!
{% else %}
It's something else
{% endif %}
Note
Check out the Twig documentation page for more details about the if
statement.
More Examples¶
If not empty¶
Only show the Repo excerpt if it is not empty.
{% if repo_excerpt is not empty %}This is the excerpt: {{ repo_excerpt }}{% endif %}
If empty¶
Check if [post_editlink] is empty in case of the user’s permission to edit a post has exceeded in the meantime.
{% if EditorialReviewsContent is empty %}
This product does not have an editorial review.
{% endif %}
If in¶
Check if the product’s languages contain “English”. You can use in
with any placeholder of type “Array”. Makes use of the Containment operator.
{% if "English" in Languages %} This product is available in english {% endif %}
Custom Fields¶
The combination of conditional statements and WordPress’ Custom Fields is real power!
For example, if you have setup a Custom Field for Products items called comment_from_reviewer, you can output it only if a reviewer entered some text.
{% if repo_custom_field_comment_from_reviewer is not empty %}
Comment from reviewer: {{ repo_custom_field_comment_from_reviewer }}
{% endif %}
For more information about the use of Custom Fields check Custom Fields.