If you have any evil plans for a switch statement for Django (hia rjwittams! ), you might want to look into my TagLib. There is a case/when/otherwise statement in there. It's quite easy to use:
{% case variable %}
{% when "value1" %}
{% endwhen %}
{% when "value2" %}
{% endwhen %}
{% otherwise %}
{% endotherwise %}
{% endcase %}
The reason for the tag structure is that the django template parser only looks for parameterless block-closing tags in the parsefor function and so you can't just pull an easy one like this:
{% if condition %}
{% elif condition %}
{% else %}
{% endif %}
You would have to copy over much from the template parser to get a parsefor that looks for a token with a tag and parameters to close the current block.
So I opted for the scoped tags approach where the "case" tag only sets up a context variable "case" and populates it with a dictionary with "value" and "fired" - with the latter one a trigger that can be fired by any "when" tag to prevent other "when" tags or the "otherwise" tag to fire themselves. A bit ugly, but working.
rjwittams Oct. 30, 2005, 2:33 p.m.
Hey, thats close to what I was going to do ;-)
Not sure about the name though.
I would think choose would be better : it would match xslt then.
Case implies one case, eg one branch of a switch statement.
Although this is a lot closer to a C switch than xslt choose as it doesn't handle arbitrary conditions, just equality. Not sure if arbitrary conditions would fit in django.