if
If a particular condition is evaluated to be truthy
, output the encapsulated block.
liquid
{% if condition %}
block_to_output
{% endif %}
1
2
3
2
3
condition
: Truthy condition to evaluate.block_to_output
: Rendered expression in case the condition is met.
Example
Data
json
{
"product": {
"track_inventory": true
}
}
1
2
3
4
5
2
3
4
5
Note: Data is only refereed as a reference for this example and is in no means reflecting real world data.
liquid
{% if product.track_inventory == true %}
<span>There only 15 items left. Hurry up!</span>
{% endif %}
1
2
3
2
3
Output
There only 15 items left. Hurry up!
elseif
elseif
statement can be used in cases where you want to extend the if
statement by specifying another condition if the preceding condition is falsy
.
Example
Data
json
{
"product": {
"track_inventory": true,
"price": 100,
"compare_at_price": 140,
"slug": "youcan"
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
Note: Data is only refereed as a reference for this example and is in no means reflecting real world data.
liquid
{% if product.track_inventory == false %}
First condition is truthy
{% elseif product.price < product.compare_at_price %}
First condition is falsy, and the second condition is truthy
{% elseif product.slug == 'youcan' %}
If both the first and second conditions are both falsy, and the third one is truthy
{% endif %}
1
2
3
4
5
6
7
2
3
4
5
6
7
Output
First condition is false, and the second condition is truthy