Opened 16 years ago
Closed 13 years ago
#7711 closed New feature (wontfix)
Switch/case tag
Reported by: | Gabriel Falcão | Owned by: | nobody |
---|---|---|---|
Component: | Template system | Version: | dev |
Severity: | Normal | Keywords: | case switch tags template templatetag |
Cc: | simon@… | Triage Stage: | Design decision needed |
Has patch: | yes | Needs documentation: | yes |
Needs tests: | yes | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
Django is kinda pythonic and mostly of conditional tags implemented are what mostly people would need:
{% if %}, {% ifequal %}, {% ifnotequal %} and so...
But i think that sometimes a "switch tag" would be great!
So i've implemented it, and attached the patch with this ticket
The implementation can be used as this:
{% switch person.name %} {% case "John Doe" %} My name is Doe, John Doe {% endcase %} {% case "Mary Jane" %} My name is Mary Jane {% endcase %} {% default %} OMG! I have no name! {% endswitch %}
Attachments (2)
Change History (11)
by , 16 years ago
Attachment: | new_switch_tag_enhancement.patch added |
---|
comment:1 by , 16 years ago
Patch needs improvement: | set |
---|
Better add {% enddefault %} for symmetry and consistency.
This part in do_case() is wrong:
nodelist_true = parser.parse(('else', end_tag)) token = parser.next_token() if token.contents == 'else': nodelist_false = parser.parse((end_tag,)) parser.delete_first_token() else: nodelist_false = NodeList() return CaseNode(bits[1], nodelist_true)
You are using nodelist_false nowhere in do_case().
comment:2 by , 16 years ago
Needs tests: | set |
---|
comment:3 by , 16 years ago
Sending a new patch:
- Removes the the useless nodelist_false in do_case().
- Adds support to use nested
{% switch %}
tags.
by , 16 years ago
Attachment: | improving_the_context_filling.patch added |
---|
Adding support to nested switches
comment:4 by , 16 years ago
milestone: | → post-1.0 |
---|---|
Triage Stage: | Unreviewed → Design decision needed |
I think that the ifgreaterthan, ifstartswith and some other tags are more important than the switch one, and are not included, so probably you'll have to use it as an external application.
Anyway I'll keep this ticket opened for revision.
comment:5 by , 16 years ago
Needs documentation: | set |
---|
comment:7 by , 14 years ago
Cc: | added |
---|---|
Summary: | Implement a tag "Switch" for conditional rendering → Switch/case tag |
Having a case/switch tag is a good call. Rendering different blocks for multiple conditions is common enough, and using multiple if's is a hack. We aren't in Python land here anymore toto, this is dirty old html.
This one looks pretty tidy http://djangosnippets.org/snippets/967/ only it needs to be updated to use the new "if" syntax conditions.
comment:8 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → New feature |
comment:9 by , 13 years ago
Easy pickings: | unset |
---|---|
Resolution: | → wontfix |
Status: | new → closed |
UI/UX: | unset |
I'm -1 on this and marking it WONTFIX - it's incredibly verbose, uses a syntax that isn't even in normal Python, and has a usecase that would be much better served by an {% elif condition %} tag, a feature which is awaiting the closure of #3100.
A proposal os implementation