Skip to main content

else

Allows you to specify a default expression to execute when a for loop has zero length.

Syntax

{% for variable in array %}
first_expression
{% else %}
second_expression
{% endfor %}
variable

The current item in the array.

array

The array to iterate over.

first_expression

The expression to render for each iteration.

second_expression

The expression to render if the loop has zero length.

{% for product in collection.products %}
{{ product.title }}<br>
{% else %}
There are no products in this collection.
{% endfor %}

Output

There are no products in this collection.
Was this page helpful?