Re: break/continue for templates
Hallöchen! George Sakkis writes: > On Jul 5, 2:25 pm, Torsten Bronger> wrote: > >> [...] >> >> People will want to do *everything* in the template. The lowest >> rated snippet on djangosnippets currently is an {% exec %} >> tag. :-) > > But then again there is a {% switch %} tag [1] that has 8/8 > positive votes, let alone the "smart" {% if %} tag that is at last > part of Django now. The question is whether non-programmers can handle it. In this context, a smart if was a good thing to do because it just made a necessary control structure more intuitive. But the other things are simply bringing full-blown programming into templates. Besides, it is *very* easy to migrate them into the Python code. Tschö, Torsten. -- Torsten Bronger, aquisgrana, europa vetus Jabber ID: torsten.bron...@jabber.rwth-aachen.de or http://bronger-jmp.appspot.com -- 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.
Re: break/continue for templates
On Jul 5, 2:25 pm, Torsten Brongerwrote: > Hall chen! > > George Sakkis writes: > > [...] > > > To be honest, I never *had to* do it (in the strict sense) either > > but apparently others did ([1-4]). As for the "just put it in the > > view" argument, remember that until last month this was the > > response for people wanting "if" to be a bit smarter :) > > People will want to do *everything* in the template. The lowest > rated snippet on djangosnippets currently is an {% exec %} tag. :-) But then again there is a {% switch %} tag [1] that has 8/8 positive votes, let alone the "smart" {% if %} tag that is at last part of Django now. I'm curious if break/continue or the just posted {% while %} tag [2] will end up closer to switch or to exec :-) George [1] http://djangosnippets.org/snippets/967/ [2] http://djangosnippets.org/snippets/2096/ -- 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.
Re: break/continue for templates
Hallöchen! George Sakkis writes: > [...] > > To be honest, I never *had to* do it (in the strict sense) either > but apparently others did ([1-4]). As for the "just put it in the > view" argument, remember that until last month this was the > response for people wanting "if" to be a bit smarter :) People will want to do *everything* in the template. The lowest rated snippet on djangosnippets currently is an {% exec %} tag. :-) Therefore, it's important to enforce some discipline, so that the templates don't become sort of PHP. I use "break" and "continue" a lot, but I've never thought about it in templates. You can easily prepare the list in the Python land. Tschö, Torsten. -- Torsten Bronger, aquisgrana, europa vetus Jabber ID: torsten.bron...@jabber.rwth-aachen.de or http://bronger-jmp.appspot.com -- 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.
Re: break/continue for templates
On Jul 5, 10:17 am, "euan.godd...@googlemail.com"wrote: > The link to your snippet doesn't work for me. Sorry, it was accidentally double posted and the original was deleted. Here it is now: http://djangosnippets.org/snippets/2093/. > It sounds like a neat idea. However, I've never come across a > situation where I've needed to do that in 2.5 years of developing > Django templates, and, whilst I agree that we don't want to get into a > holy war about whether stuff should be done in templates or views, I > reckon the reason I've avoided this sort of thing is because I must > have done that sort of work in the view :) To be honest, I never *had to* do it (in the strict sense) either but apparently others did ([1-4]). As for the "just put it in the view" argument, remember that until last month this was the response for people wanting "if" to be a bit smarter :) George [1] http://bit.ly/9ordv7 [2] http://bit.ly/9B0b89 [3] http://bit.ly/aldiqA [4] http://bit.ly/bXMmap -- 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.
Re: break/continue for templates
The link to your snippet doesn't work for me. It sounds like a neat idea. However, I've never come across a situation where I've needed to do that in 2.5 years of developing Django templates, and, whilst I agree that we don't want to get into a holy war about whether stuff should be done in templates or views, I reckon the reason I've avoided this sort of thing is because I must have done that sort of work in the view :) Euan On Jul 4, 9:03 pm, George Sakkiswrote: > 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 %} > {% for value in values %} > {{ key }}: {{ value }} > {% if value|divisibleby:3 %} > {{ value }} is divisible by 3 > {{ forloop.parentloop|continue }} > {% endif %} > {% endfor %} > {{ key }}: No value divisible by 3 > {% 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.
break/continue for templates
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 %} {% for value in values %} {{ key }}: {{ value }} {% if value|divisibleby:3 %} {{ value }} is divisible by 3 {{ forloop.parentloop|continue }} {% endif %} {% endfor %} {{ key }}: No value divisible by 3 {% 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.