Opened 15 years ago
Closed 15 years ago
#11799 closed (duplicate)
shortcoming in template language 'if' statement
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Template system | Version: | 1.0 |
Severity: | Keywords: | if ifequal inefficient | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Hi there,
I'm pretty new to django so chances are that my remark is way out of line, but it seems there is a shortcoming in the
django template language that forces you to do a lot of extra typing or do really ugly includes.
The basic problem I have is this:
Depending on the contents of a field in an object *and* on a boolean variable passed to the template I have to display an object from an object_list one way or the other.
The only way that I've found to make this work looks like this:
{% if titleonly %} <!-- threads list header --> {% ifequal item.itemtype 0 %} <!-- threads list header --> <h3>{{ item.title }}</h3><hr /> {% else %} <!-- threads list item --> {% include "items/item_row.html" %} {% endifequal %} {% else %} {% include "items/item_row.html" %} {% endif %}
The include is there to avoid really bad repetition of template code.
Ideally this should have looked like:
{% ifequal titleonly True and item.itemtype 0 %} <h3>{{ item.title }}</h3><hr /> {% else %} {% include "items/item_row.html" %} {% endif %}
The include could have been replaced in this example by the actual code to display the item.
Am I doing something wrong or is this 'by design', and if so what is the motivation for crippling the
template language in such a way that tons of special cases have to be worded as statements instead of
providing a good general purpose tool ?
If what you want is an ifequal tag that supports and/or, that's a dupe of #9447.
For feedback on other approaches, design rationales, etc., django-users would be a better place than trac.