break

Immediately exit a running for loop when a specific condition is met, stopping all further iterations.

Syntax

{% for item in array %} {% if condition %} {% break %} {% endif %} ... {% endfor %}

Example

{% assign nums = "1,2,3,4,5" | split: "," %}{% for n in nums %}{% if n == "3" %}{% break %}{% endif %}{{ n }}{% endfor %}

Output

12

The break tag causes a for loop to stop execution immediately, discarding remaining items and proceeding to the markup following endfor. It is typically combined with an if statement to cap display items.

In programmatic SEO templates, break limits the number of internal cross-links rendered from sibling data. For example: {% for related in page.related %}{% if forloop.index > 4 %}{% break %}{% endif %}<a href="{{ related.path }}">{{ related.title }}</a>{% endfor %}. This keeps navigation focused on the top four nearest neighbors.

Try this syntax in the editor.