Code

Opened 7 years ago

Closed 7 years ago

Last modified 7 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: UI/UX:

Description

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>
    <ul>
    {% for item in item_list|slice:"1:" %}
        <li>{{ item.title }}</li>
    {% endfor %}
    </ul>

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.

Attachments (0)

Change History (2)

comment:1 Changed 7 years ago by gwilson

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

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

comment:2 Changed 7 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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.