Code

Opened 7 years ago

Closed 7 years ago

#3791 closed (wontfix)

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

Reported by: nowell strite Owned by: adrian
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 7 years ago.
Patch for django's If templatetag

Download all attachments as: .zip

Change History (4)

Changed 7 years ago by nowell strite

Patch for django's If templatetag

comment:1 Changed 7 years ago by nowell strite

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 7 years ago by Simon G. <dev@…>

  • Triage Stage changed from Unreviewed to Design decision needed

comment:3 Changed 7 years ago by jacob

  • Resolution set to wontfix
  • Status changed from new to closed

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).

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.