Opened 15 years ago

Closed 15 years ago

#10821 closed (duplicate)

[patch] Add new template tag to test 'foo in bar'

Reported by: tomevans222 Owned by: nobody
Component: Template system Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

There is currently no way for a template to test whether a value is in a list or not.

This is quite a useful tag to have, when drawing tables for example:

   ctxt = { 'my_colours': ['orange', 'yellow'], 
            'all_colours': ['red', 'orange', 'yellow', 'green', blue', 'indigo', 'violet'] }
 <tr>{% for clr in all_colours %}<th>{{ clr }}</th>{% endfor %}</tr>
 <tr>
   {% for clr in all_colours %}
   <td>{% IfInList clr in my_colours %}I like it{% else %}I hate it{% endif %}</td>
   {% endfor %}
 </tr>

Attached file adds functionality, but (as I need this now) it follows the style I use for my supplementary tag libraries (camel case node name, 2 space indents, standalone) so would need to be polished slightly to go in django/template/defaulttags.py

Attachments (1)

if_in_list_tag.py (2.8 KB ) - added by tomevans222 15 years ago.
Implementation of IfInList tag

Download all attachments as: .zip

Change History (2)

by tomevans222, 15 years ago

Attachment: if_in_list_tag.py added

Implementation of IfInList tag

comment:1 by Karen Tracey, 15 years ago

Resolution: duplicate
Status: newclosed

ifin and ifnotin already proposed by #8087.

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