<p>Currently there is no special build-in command to do it (unless you want to use a plugin, as <a href="https://craftcms.stackexchange.com/a/864/250">stated</a> by Brandon Kelly). There is a workaround, however. The workaround is to check for additional condition.</p>

<pre><code>{% set break = 0 %}

{# this variable we will set to 1 when we do not want code in our loop to run anymore #}

{% for i in 20..100 if break == 0 %} 
    ...
    {# on some condition we will set break to 1 to prevent running of further code #}
    {% set break = 1 %}
    ...
{% endfor %}
</code></pre>

CC BY-SA 3.0
2014-07-09T16:50:51.217Z
Interesting. Apparently [you can use Twig conditional filters](http://stackoverflow.com/questions/21672796/how-can-i-use-break-or-continue-within-for-in-twig-template) as well. Have you run into a practical need for this, or is this a theoretical question?
Matt Stein
CC BY-SA 3.0
2014-07-09T16:53:51.863Z
@Matt Didn't know about conditional filters.  About practical need - yes, finding if number is prime ;-) And nobody forbid you to post an answer about using filters to break from a loop (I won't edit mine because its *your* idea) :-)
nicael
CC BY-SA 3.0
2014-07-09T17:29:15.377Z
For reference, it doesn't look like break or continue will ever be part of Twig: https://github.com/fabpot/Twig/issues/654
Brad Bell
CC BY-SA 3.0
2014-07-09T17:30:46.210Z
@Brad So exclude "currently"?
nicael
CC BY-SA 3.0
2014-07-09T17:32:46.520Z
@nicael - It's still technically accurate. There's always the chance it being missing will annoy us enough that we'll add it in Craft, like we did with {% switch %}.
Brad Bell
CC BY-SA 3.0
2014-07-09T16:38:32.347Z
2014-07-09T18:08:22.367Z
2017-04-13T12:51:00.160Z
nicael
<p>Marion Newlevant wrote a Craft plugin that adds <code>{% break %}</code> and <code>{% continue %}</code> tags to Twig:</p>

<p><a href="https://github.com/marionnewlevant/craft-mnbreakandcontinue" rel="nofollow noreferrer">https://github.com/marionnewlevant/craft-mnbreakandcontinue</a></p>

<p>The Craft 3 version is <a href="https://plugins.craftcms.com/twig-perversion" rel="nofollow noreferrer">Twig Perversion</a></p>

CC BY-SA 3.0
2014-07-09T18:10:11.927Z
@nicael certainly not, your answer is definitely the best way to do it without any plugins.
Brandon Kelly
CC BY-SA 4.0
2014-07-09T17:39:24.967Z
2019-05-15T05:27:24.497Z
2019-05-15T05:27:24.497Z
Brandon Kelly
<p>Is it possible to break out of a loop in twig? If it is, how would I do it?</p>

<templating><forloop>
Is it possible to break out of a loop in twig?
# Is it possible to break out of a loop in twig?

(Asked by: username_0 on 2014-07-09)

Is it possible to break out of a loop in twig? If it is, how would I do it?

**Tags:** templating, forloop

---

## 2 answers

### Answer by username_3 on 2014-07-09. Score: 8 | Accepted answer

Marion Newlevant wrote a Craft plugin that adds `{% break %}` and `{% continue %}` tags to Twig:

<https://github.com/marionnewlevant/craft-mnbreakandcontinue>

The Craft 3 version is [Twig Perversion](https://plugins.craftcms.com/twig-perversion)

> **Comments:**
> **username_3**: @nicael certainly not, your answer is definitely the best way to do it without any plugins.  

---

### Answer by username_0 on 2014-07-09. Score: 16

Currently there is no special build-in command to do it (unless you want to use a plugin, as stated by Brandon Kelly). There is a workaround, however. The workaround is to check for additional condition.
{% set break = 0 %}

{# this variable we will set to 1 when we do not want code in our loop to run anymore #}

{% for i in 20..100 if break == 0 %} 
    ...
    {# on some condition we will set break to 1 to prevent running of further code #}
    {% set break = 1 %}
    ...
{% endfor %}

> **Comments:**
> **username_1**: Interesting. Apparently [you can use Twig conditional filters](http://stackoverflow.com/questions/21672796/how-can-i-use-break-or-continue-within-for-in-twig-template) as well. Have you run into a practical need for this, or is this a theoretical question?  
> **username_0**: @Matt Didn't know about conditional filters.  About practical need - yes, finding if number is prime ;-) And nobody forbid you to post an answer about using filters to break from a loop (I won't edit mine because its *your* idea) :-)  
> **username_2**: For reference, it doesn't look like break or continue will ever be part of Twig: https://github.com/fabpot/Twig/issues/654  
> **username_0**: @Brad So exclude "currently"?  
> **username_2**: @nicael - It's still technically accurate. There's always the chance it being missing will annoy us enough that we'll add it in Craft, like we did with {% switch %}.  

---

