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)

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

Download all attachments as: .zip

Change History (19)

Changed 15 years ago by Michał Sałaban

comment:1 Changed 15 years ago by dc

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 Changed 15 years ago by Michał Sałaban

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 Eric Holscher

Triage Stage: UnreviewedDesign decision needed

comment:4 Changed 14 years ago by Dana Spiegel

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 Dagur Páll Ammendrup

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

comment:6 Changed 14 years ago by (none)

milestone: post-1.0

Milestone post-1.0 deleted

comment:7 Changed 14 years ago by Karen Tracey

#10821 asked for ifin again.

comment:8 in reply to:  2 Changed 14 years ago by dc

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 dc

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 tomevans222

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 dc

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

Good idea.

comment:12 Changed 14 years ago by tomevans222

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 tomevans222

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

comment:13 Changed 14 years ago by anonymous

tom: fantastic, exactly what I need. :)

comment:14 Changed 14 years ago by krystal

This patch should be _very_ useful ...

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

comment:15 Changed 14 years ago by tomevans222

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 tomevans222

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 Changed 13 years ago by Johannes Dollinger

Resolution: fixed
Status: newclosed

Fixed in [11806].

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