Opened 6 years ago

Closed 6 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: UI/UX:

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 6 years ago.
Implementation of IfInList tag

Download all attachments as: .zip

Change History (2)

Changed 6 years ago by tomevans222

Implementation of IfInList tag

comment:1 Changed 6 years ago by kmtracey

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

ifin and ifnotin already proposed by #8087.

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