Opened 18 years ago
Closed 18 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: | dev |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Design decision needed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
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)
Change History (4)
by , 18 years ago
Attachment: | if_templatetag.diff added |
---|
comment:1 by , 18 years ago
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 by , 18 years ago
Triage Stage: | Unreviewed → Design decision needed |
---|
comment:3 by , 18 years ago
Resolution: | → wontfix |
---|---|
Status: | new → 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).
Patch for django's If templatetag