Opened 8 years ago

Closed 5 years ago

#7711 closed New feature (wontfix)

Switch/case tag

Reported by: Gabriel Falcão Owned by: nobody
Component: Template system Version: master
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


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 %}
    {% 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 8 years ago.
A proposal os implementation
improving_the_context_filling.patch (5.0 KB) - added by Gabriel Falcão 8 years ago.
Adding support to nested switches

Download all attachments as: .zip

Change History (11)

Changed 8 years ago by Gabriel Falcão

A proposal os implementation

comment:1 Changed 8 years ago by dc

Needs documentation: unset
Needs tests: unset
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,)) 
    nodelist_false = NodeList()
return CaseNode(bits[1], nodelist_true)

You are using nodelist_false nowhere in do_case().

comment:2 Changed 8 years ago by dc

Needs tests: set

comment:3 Changed 8 years ago by Gabriel Falcão

Sending a new patch:

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

Changed 8 years ago by Gabriel Falcão

Adding support to nested switches

comment:4 Changed 8 years ago by Marc Garcia

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 Changed 8 years ago by Marc Garcia

Needs documentation: set

comment:6 Changed 8 years ago by (none)

milestone: post-1.0

Milestone post-1.0 deleted

comment:7 Changed 6 years ago by Simon Litchfield

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 only it needs to be updated to use the new "if" syntax conditions.

comment:8 Changed 6 years ago by Luke Plant

Severity: Normal
Type: New feature

comment:9 Changed 5 years ago by Andrew Godwin

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