Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#6187 closed (worksforme)

FOR with filters, like slice, would be oh so nice

Reported by: Rob Hudson <treborhudson@…> Owned by: nobody
Component: Template system Version: master
Severity: Keywords:
Cc: treborhudson@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


This would be handy to be able to do:

    {% for item in item_list|slice:"1:" %}
    {% endfor %}

I think it would require the for template tag to be pre-processed through whatever filter prior to iteration, correct?

The use case I was just working against was that I wanted to treat the first item in the list differently without having for loop logic. For example, something along these lines:

    <h3>{{ item_list.0.title }}</h3>
    <p>{{ item_list.0.body }}</p>
    <h4>Other items</h4>
    {% for item in item_list|slice:"1:" %}
        <li>{{ item.title }}</li>
    {% endfor %}

I realize maybe the example might be contrived, but for those filters that return a list (slice, make_list, others?) it also seems like a valid use that could come in handy.

Change History (2)

comment:1 Changed 11 years ago by Gary Wilson

Resolution: worksforme
Status: newclosed

Doesn't it already do this? Does for me anyway.

comment:2 Changed 11 years ago by Rob Hudson

This appears to work for me if I wrap the queryset with a list() call. If I don't, the slice does nothing.

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