Skip to content

if ​

Last updated View as Markdown

If a particular condition is evaluated to be truthy, output the encapsulated block.

liquid
{% if condition %}
  block_to_output
{% endif %}
  • condition: Truthy condition to evaluate.
  • block_to_output: Rendered expression in case the condition is met.

Example

Data
json
{
  "product": {
    "track_inventory": true
  }
}

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 %}

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"
  }
}

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 %}

Output

First condition is false, and the second condition is truthy