Code

Opened 6 years ago

Closed 3 years ago

#7711 closed New feature (wontfix)

Switch/case tag

Reported by: GabrielFalcao 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

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 GabrielFalcao 6 years ago.
A proposal os implementation
improving_the_context_filling.patch (5.0 KB) - added by GabrielFalcao 6 years ago.
Adding support to nested switches

Download all attachments as: .zip

Change History (11)

Changed 6 years ago by GabrielFalcao

A proposal os implementation

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

  • Needs tests set

comment:3 Changed 6 years ago by GabrielFalcao

Sending a new patch:

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

Changed 6 years ago by GabrielFalcao

Adding support to nested switches

comment:4 Changed 6 years ago by garcia_marc

  • milestone set to post-1.0
  • Triage Stage changed from Unreviewed to 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 Changed 6 years ago by garcia_marc

  • Needs documentation set

comment:6 Changed 5 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

comment:7 Changed 3 years ago by simon29

  • Cc simon@… added
  • Summary changed from Implement a tag "Switch" for conditional rendering to 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 Changed 3 years ago by lukeplant

  • Severity set to Normal
  • Type set to New feature

comment:9 Changed 3 years ago by andrewgodwin

  • Easy pickings unset
  • Resolution set to wontfix
  • Status changed from new to 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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.