Opened 10 years ago

Closed 10 years ago

#3791 closed (wontfix)

Enhance functionality of {% if ... %} tag in a Django way.

Reported by: nowell strite Owned by: Adrian Holovaty
Component: Template system Version: master
Severity: Keywords:
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

I understand the desire to provide templatetags to allow designers or non-programmers an easy interface for writing conditional statements in a template, and for not exposing too much logic at the template level. However, if only to combine those pesky tags, and to allow a more robust conditional evaluation system, I modified the existing IfNode/do_if functions to allow for more useful conditional evaluations.

This patch allows conditional expressions that check for:

  • Equallity -- i == 1, i != 1
  • Greater/Less than -- i >= 1, i > 1, i < 1, i <= 1
  • Nested conditionals -- i >= 0 and i <= 10
  • Mixed and/or -- i == 15 or (i >= 0 and i <= 10)
  • List contains -- i in ('Apple', 'Orange', 'Pear'), 'Pear' in i
  • Negation -- i not in ('Apple', 'Orange', 'Pear'), not i

This keeps with the concept that we should only allow access to variables/dicts/objects/arrays in a Django template way, it resolves the value of variables (and only accepts Django Template syntax) in the same way all tags do, so this does not open up random python code execution.

{% if food in ("Steak", "Pizza", "Salad", food_of_the_day) and (restaurant.meal == 'Lunch' or restaurant.customers >= 50) %}
    Might as well open up and feed them lunch!
{% else %}
    We're closed!
{% endif %}

Attachments (1)

if_templatetag.diff (5.3 KB) - added by nowell strite 10 years ago.
Patch for django's If templatetag

Download all attachments as: .zip

Change History (4)

Changed 10 years ago by nowell strite

Attachment: if_templatetag.diff added

Patch for django's If templatetag

comment:1 Changed 10 years ago by nowell strite

I would be willing to write regression tests and documentation for this, assuming it is an idea/patch that is acceptable to the community.

comment:2 Changed 10 years ago by Simon G. <dev@…>

Triage Stage: UnreviewedDesign decision needed

comment:3 Changed 10 years ago by Jacob

Resolution: wontfix
Status: newclosed

This comes up from time to time -- search the tickets or the development mailing lists for the nitty details. As I've said before, this is directly against the design of Django's template system, and isn't going to be part of Django. To pick just a couple of the many reasons to reject this feature: (a) this complicates the template system for template authors and (b) this type of logic belongs in your view anyway.

It's probably a good idea to upload this tag to djangosnippets.org, though; someone else may find it useful (and doing so could forstall the 1045th rehash of this same request).

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