The Liquid Logical Statements are useful if you want to check some variable before displaying it or if you want to create a specific list based on another object.
If Statement
The If Statement allows you to verify the object value when needed:
{% if question.name == "Getting Started" %}
<p>Only printed if the question name is "Getting Started".</p>
{% endif %}If you just need to check on a value before printing a default text, you'd need to use the above example. But if you have a default text and want a different one in just a specific case, the best approach is the following one:
{% if question.name == "Getting Started" %}
<p>Only printed if the question name is "Getting Started".</p>
{% else %}
<p>This would be the default text, i.e when the question name differs from "Getting Started"</p>
{% endif %}Notice that the If Statement must be rounded by "{%" and "%}". Also, it needs to be closed properly:
{% if ... %}
...
{% endif %}
For Statement
The For Statement allows you to loop through a list of objects. For instance, if you want to loop through the question list from the current category, you can use:
{% for question in category.questions %}
<h2>{{ question.name }}</h2>
<a href="{{ question.url }}">{{ question.name }}</a>
{% endfor %}Another example:
<ul class="top_categories">
{% for categories in roots_categories %}
<li class="category_item">
<a href="{{ category.url }}">{{ category.name }}</a>
</li>
{% endfor %}
</ul>
Notice that the For Statement must be rounded by "{%" and "%}". Also, it needs to be closed properly:
{% for ... %}
...
{% endfor %}
Real Liquid Example
This example is from a Standard Helpjuice Theme live on our Theme Library:
<ul class="main_navigation">
{% for category in roots_categories %}
<li><a href="{{ category.url }}">{{ category.name }}</a></li>
{% if category.ordered_subcategories.size > 0 %}
<ul class="subcategories">
{% for subcategory in category.ordered_subcategories %}
<li>
<a href="{{ subcategory.url }}">{{ subcategory.name }}</a>
</li>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
</ul>
Notice that you can have nested For Statements.
Liquid Filters
You can apply filters on the Liquid Statements. Every time you need to limit a loop statement or change the output of an object value, you can rely on Liquid Filters. Check the Shopify/Liquid Filter Syntax article.
Objects List
You can find the complete object reference at Shopify/Liquid Complete Objects Reference.
Advanced Shopify/Liquid Syntax
You can find more information about Logical Statements on the official Liquid documentation. The complete Shopify/Liquid documentation is located here.