Opened 16 years ago
Closed 15 years ago
#8087 closed (fixed)
Template tags 'ifin' and 'ifnotin' checking if element is present in sequence
Reported by: | Michał Sałaban | Owned by: | nobody |
---|---|---|---|
Component: | Template system | Version: | dev |
Severity: | Keywords: | templatetag, tag, sequence | |
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
This is a proposal for new templatetags ifin and ifnotin which check if an element is present in sequence.
Example:
<ul> {% for member in members_list %} <li> {{member.username}} {% ifin member user.friends_set.all %} <p>This user is your friend!</p> {% endifin %} </li> {% endfor %} </ul>
Attachments (2)
Change History (19)
by , 16 years ago
Attachment: | django-ifin-templatetag.patch added |
---|
comment:1 by , 16 years ago
follow-up: 8 comment:2 by , 16 years ago
This is a good way to do it too, but I wonder what other use the contains filter could have, besides:
{{ user.friends_set.all|contains:member|yesno }}
In general: filters returning boolean values don't seem to be very useful in other situations.
comment:3 by , 16 years ago
Triage Stage: | Unreviewed → Design decision needed |
---|
comment:4 by , 16 years ago
I definitely vote for a ifin tag, and not a filter. I have iteration logic for displaying form fields that relies on checking whether a field is defined in a pre-defined list of fields before it is rendered in the template. The ifin and ifnotin tags are the only easy way to make this work properly without rewriting a new iterator in the form.
comment:5 by , 16 years ago
Why hasn't this been done in Django already. I mean, what are the arguments against it?
comment:8 by , 16 years ago
In general: filters returning boolean values don't seem to be very useful in other situations.
'ifin' tag also will be usable only in one situation.
Why create new unnecessary construct with complicated implementation that duplicates 'if' tag code when problem can be solved with a simple filter and existing 'if' tag?
And there already are boolean filters 'divisibleby' and 'length_is'.
Anyway i'm strong -1 on current syntax, better change it to the {% ifin sequence item %} form.
I definitely vote for a ifin tag, and not a filter.
I don't see why your problem can not be solved with a filter.
comment:9 by , 16 years ago
With new tag it is also hard to do: {% if user.is_anonymous or ban_list|contains:user %}
comment:10 by , 16 years ago
I was the submitter of #10821, and the more I think on it, the more a separate tag seems ridiculous. Surely this behaviour should be supported in the default if tag?
{% if foo in bar or a_bool or a_list %}{% wibble %}{% endif %}
dc: This would make your hard to do example:
{% if user.is_anonymous or user in ban_list %}
Which do you think is easier to read and has a clearer intention?
I will prepare a patch that achieves this.
Cheers
comment:11 by , 16 years ago
Surely this behaviour should be supported in the default if tag?
Good idea.
comment:12 by , 16 years ago
Hi all
I've reworked this into a patch (to trunk, r10604) that enhances the regular if tag to add this feature.
What do you think?
Cheers
Tom
by , 16 years ago
Attachment: | django-if-in-list.diff added |
---|
comment:14 by , 16 years ago
This patch should be _very_ useful ...
Is there any chance it get included it 1.1 ? :)
comment:15 by , 16 years ago
I think it is WAY too late to make it in 1.1 :)
After 1.1 is released, I intend to try to help get this added in one way or another, so maybe 1.2 :)
comment:16 by , 15 years ago
Ping. Can a committer take a look at this and advise me what changes would need to be made to my diff in order to be accepted.
Cheers
Tom
Does we really need separate template tag for everything? I think better fix filter handling in tags and use