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)

new_switch_tag_enhancement.patch (4.6 KB ) - added by Gabriel Falcão 16 years ago.
A proposal os implementation
improving_the_context_filling.patch (5.0 KB ) - added by Gabriel Falcão 16 years ago.
Adding support to nested switches

Download all attachments as: .zip

Change History (11)

by Gabriel Falcão, 16 years ago

A proposal os implementation

comment:1 by dc, 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 dc, 16 years ago

Needs tests: set

comment:3 by Gabriel Falcão, 16 years ago

Sending a new patch:

  • Removes the the useless nodelist_false in do_case().
  • Adds support to use nested {% switch %} tags.

by Gabriel Falcão, 16 years ago

Adding support to nested switches

comment:4 by Marc Garcia, 16 years ago

milestone: post-1.0
Triage Stage: UnreviewedDesign 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 Marc Garcia, 16 years ago

Needs documentation: set

comment:6 by (none), 16 years ago

milestone: post-1.0

Milestone post-1.0 deleted

comment:7 by Simon Litchfield, 14 years ago

Cc: simon@… added
Summary: Implement a tag "Switch" for conditional renderingSwitch/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 Luke Plant, 14 years ago

Severity: Normal
Type: New feature

comment:9 by Andrew Godwin, 13 years ago

Easy pickings: unset
Resolution: wontfix
Status: newclosed
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.

Note: See TracTickets for help on using tickets.
Back to Top