Opened 15 years ago
Closed 13 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)
Changed 15 years ago by
Attachment: | django-ifin-templatetag.patch added |
---|
comment:1 Changed 15 years ago by
comment:2 follow-up: 8 Changed 15 years ago by
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 Changed 15 years ago by
Triage Stage: | Unreviewed → Design decision needed |
---|
comment:4 Changed 14 years ago by
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 Changed 14 years ago by
Why hasn't this been done in Django already. I mean, what are the arguments against it?
comment:8 Changed 14 years ago by
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 Changed 14 years ago by
With new tag it is also hard to do: {% if user.is_anonymous or ban_list|contains:user %}
comment:10 Changed 14 years ago by
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 Changed 14 years ago by
Surely this behaviour should be supported in the default if tag?
Good idea.
comment:12 Changed 14 years ago by
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
Changed 14 years ago by
Attachment: | django-if-in-list.diff added |
---|
comment:14 Changed 14 years ago by
This patch should be _very_ useful ...
Is there any chance it get included it 1.1 ? :)
comment:15 Changed 14 years ago by
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 Changed 14 years ago by
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