Django snippets: "for" template tag with support for "else" if array is empty. A neat solution to a common pattern; I’d personally like to see this included in Django proper.
Django snippets: "for" template tag with support for "else" if array is empty. A neat solution to a common pattern; I’d personally like to see this included in Django proper.
Agreed, it's a very common pattern in my code.
I thought it was cool myself, but the alternative isn't very difficult (just adding an if block around the for loop).
{% else %}over an extra{% if %}...{% endif %}block to keep track of.While I found it initially interesting, I realize I wouldn't use it often :
The comon pattern is to have a <ul/li> with the {% for %} loop inside of the <ul>, so I generally tend to replace the whole list with a sentence (or remove it and the <h2> tag altogether), as a <ul/li> with only one element telling there is nothing isn't very adapted. (Hope I'm clear enough)
So I wouldn't use something ofter that can be replaced by something easy (if+for) : one obvious way to do it.
Ikshefem - 17th January 2008 08:48 - #
That's *not* how the for-in-else construct works in Python, though, so maybe a different name for the empty clause would be a good idea?
Fredrik - 17th January 2008 12:00 - #
for/else works differently in python, and this would cause much confusion. Personally, I think this should be semantics in python, but it's too late for that.
I'm up for {% default %} or {% empty %} as alternatives, though
Moof - 17th January 2008 14:10 - #