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)

django-ifin-templatetag.patch (5.4 KB ) - added by Michał Sałaban 16 years ago.
django-if-in-list.diff (7.8 KB ) - added by tomevans222 16 years ago.

Download all attachments as: .zip

Change History (19)

by Michał Sałaban, 16 years ago

comment:1 by dc, 16 years ago

Does we really need separate template tag for everything? I think better fix filter handling in tags and use

{% if user.friends_set.all|contains:member %}
{% endif %}

comment:2 by Michał Sałaban, 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 Eric Holscher, 16 years ago

Triage Stage: UnreviewedDesign decision needed

comment:4 by Dana Spiegel, 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 Dagur Páll Ammendrup, 16 years ago

Why hasn't this been done in Django already. I mean, what are the arguments against it?

comment:6 by (none), 16 years ago

milestone: post-1.0

Milestone post-1.0 deleted

comment:7 by Karen Tracey, 16 years ago

#10821 asked for ifin again.

in reply to:  2 comment:8 by dc, 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 dc, 16 years ago

With new tag it is also hard to do: {% if user.is_anonymous or ban_list|contains:user %}

comment:10 by tomevans222, 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 dc, 16 years ago

Surely this behaviour should be supported in the default if tag?

Good idea.

comment:12 by tomevans222, 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 tomevans222, 16 years ago

Attachment: django-if-in-list.diff added

comment:13 by anonymous, 16 years ago

tom: fantastic, exactly what I need. :)

comment:14 by krystal, 16 years ago

This patch should be _very_ useful ...

Is there any chance it get included it 1.1 ? :)

comment:15 by tomevans222, 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 tomevans222, 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

comment:17 by Johannes Dollinger, 15 years ago

Resolution: fixed
Status: newclosed

Fixed in [11806].

Note: See TracTickets for help on using tickets.
Back to Top