Jinja’s Loop Controls: Master Your for Loops
n
for loop is one of its most powerful features, allowing you to iterate over sequences and render dynamic content. While the standard for loop is great, sometimes you need more control—the ability to stop or skip iterations based on a condition. This is where the Loop Controls extension comes in. By enabling this extension, you can use break and continue keywords directly within your templates, bringing the familiar power of Python’s loop control statements to your Jinja loops.nnn
n
What Are break and continue?
nIn programming, break and continue are essential tools for managing the flow of a loop. The break statement is used to terminate a loop entirely. When Jinja encounters a break tag, it immediately stops processing the loop, and execution continues with the code that comes after the loop. This is useful when you’ve found what you’re looking for and don’t need to check the rest of the items in the sequence.nnThe continue statement, on the other hand, is used to skip the current iteration and move on to the next one. When Jinja encounters a continue tag, it stops processing the current loop iteration and immediately starts the next one. This is perfect for skipping items that don’t meet a specific criterion without ending the entire loop.nnBy default, Jinja does not include these keywords to maintain its philosophy of keeping logic separate from presentation. The fact that they’re an extension means you have to explicitly enable them, signaling that you’re intentionally adding a layer of control flow to your templates.nn
n
n
Putting Loop Controls into Practice
nUsing break and continue is straightforward. You simply embed them within an if statement inside your for loop to define the conditions for their activation. Consider a list of users where you only want to display every other user. You can achieve this with a continue statement combined with the special loop.index variable, which tracks the current iteration (starting at 1):n
n{% for user in users %}n{%- if loop.index is even %}{% continue %}{% endif %}n<li>{{ user.name }}</li>n{% endfor %}nnIn this example, if the current loop index is even, the continue statement is triggered. The rest of the code in that iteration—the part that renders the user’s name—is skipped, and Jinja moves on to the next user. This results in a list that only contains the odd-numbered users.nnLikewise, if you need to limit the number of items displayed, you can use break. Imagine you have a large list but only want to show the first 10 items. You can use the loop.index to check the current iteration and break out of the loop once you reach your limit:n
n{% for user in users %}n{%- if loop.index >= 10 %}{% break %}{% endif %}n<li>{{ user.name }}</li>n{% endfor %}nnThis code will render the first 10 users and then stop, preventing the loop from processing the remaining items. This is far more efficient than iterating through the entire list and using an if statement to simply hide the items you don’t want to show.nn
nn
nn
break to stop a loop early and continue to skip unwanted iterations, you can write cleaner, more performant template code that is better suited for a wide range of use cases.nn