Control Flow Tags

Control Flow tags determine which block of code should be executed based on different conditions.

In this article:


if

Executes a block of code only if a certain condition is met.

{% if post.poster_image_url %} <img src="{{ post.poster_image_url }}"> {% endif %}

elsif / else

Adds more conditions within an if or unless block.

{% if current_site.products.size == 0 %} <p>There aren't any products!</p> {% elsif current_site.products.size == 1 %} <h1>Check out this cool product:</h1> <h4>{{ current_site.products.first.title }}</h4> <h5>{{ current_site.products.first.description | strip_html }}</h5> {% else %} <h1>Check out these cool products:</h1> {% for product in current_site.products %} <h4>{{ current_site.products.first.title }}</h4> <h5>{{ current_site.products.first.description | strip_html }}</h5> {% endfor %} {% endif %}

unless

Similar to if, but executes a block of code only if a certain condition is not met.

{% unless current_site.products.size > 0 %} <p>There aren't any products!</p> {% endfor %}

case/when

Creates a switch statement to compare a variable with different values. case initializes the switch statement, and when compares its values.

{% case post.downloads.size %} {% when 0 %} <p>There are no downloads.</p> {% when 1 %} <p>There is one download.</p> {% else %} <p>There are so many downloads!</p> {% endcase %}

Contact support or access our 24/7 Live Chat:

Contact Support Live Chat

Need Help? Live Chat with Us.