Hi all, there have been at least three threads in this list alone from people asking how to "break" from a for loop in templates, so the following snippet [1] might be useful to some. Leaving aside the "thou shalt not use logic in templates" religious debate, it's interesting in that it is syntactically more powerful than the respective Python statements: you can continue/break not just from the innermost loop but from outer ones too. Here's a small example:
{% for key,values in mapping.iteritems %}<br/> {% for value in values %} {{ key }}: {{ value }}<br/> {% if value|divisibleby:3 %} {{ value }} is divisible by 3<br/> {{ forloop.parentloop|continue }} {% endif %} {% endfor %} {{ key }}: No value divisible by 3<br/> {% endfor %} Given context = {'mapping': dict(a=[1,2,3], c=[2,4,5], b=[3,5,7])}, the output is: a: 1 a: 2 a: 3 3 is divisible by 3 c: 2 c: 4 c: 5 c: No value divisible by 3 b: 3 3 is divisible by 3 Tested (lightly) on Django 1.2 / Python 2.6; please let me know if you hit any bugs or unexpected behavior. Cheers, George [1] http://djangosnippets.org/snippets/2092/ -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.