Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-09T16:50:51.217Z
Answers.Text: 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?
Answers.UserDisplayName: Matt Stein
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-09T16:53:51.863Z
Answers.Text: @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) :-)
Answers.UserDisplayName: nicael
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-09T17:29:15.377Z
Answers.Text: For reference, it doesn't look like break or continue will ever be part of Twig: https://github.com/fabpot/Twig/issues/654
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-09T17:30:46.210Z
Answers.Text: @Brad So exclude "currently"?
Answers.UserDisplayName: nicael
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-09T17:32:46.520Z
Answers.Text: @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 %}.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-09T16:38:32.347Z
Answers.LastActivityDate: 2014-07-09T18:08:22.367Z
Answers.LastEditDate: 2017-04-13T12:51:00.160Z
Answers.UserDisplayName: nicael
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-09T18:10:11.927Z
Answers.Text: @nicael certainly not, your answer is definitely the best way to do it without any plugins.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2014-07-09T17:39:24.967Z
Answers.LastActivityDate: 2019-05-15T05:27:24.497Z
Answers.LastEditDate: 2019-05-15T05:27:24.497Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>Is it possible to break out of a loop in twig? If it is, how would I do it?</p>
  
Tags: <templating><forloop>
Title: Is it possible to break out of a loop in twig?
ThreadText: # 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**: @username_0 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](https://craftcms.stackexchange.com/a/864/250) by username_3). 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**: @username_1 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**: @username_2 So exclude "currently"?  
  > **username_2**: @username_0 - 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 %}.  
  
  ---
  
  