Opened 17 years ago

Closed 17 years ago

Last modified 14 years ago

#5567 closed (fixed)

Add a "last" template filter

Reported by: Chris H. Owned by: nobody
Component: Template system Version: dev
Severity: Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by James Bennett)

Hi,

It seemed like an oversight that there wasn't a "last" template filter similar to the "first" filter. I wrote it up, along with tests, and added in an nth filter that takes an arg and accessing that index of the value.

>>> last([0, 1, 2, 3, 4])
4
>>> last(u'Hello')
u'o'
>>> nth(['a', 'b', 'c', 'd'], 1)
'b'
>>> nth(u'abcd', 1)
u'b'

Attachments (3)

last_and_nth_filters.diff (1.1 KB ) - added by Chris H. 17 years ago.
filter-last.diff (886 bytes ) - added by Aaron C. de Bruyn 17 years ago.
filter-last.2.diff (1.3 KB ) - added by Aaron C. de Bruyn 17 years ago.

Download all attachments as: .zip

Change History (14)

by Chris H., 17 years ago

Attachment: last_and_nth_filters.diff added

comment:1 by James Bennett, 17 years ago

Description: modified (diff)

(cleaning up formatting in description)

comment:2 by James Bennett, 17 years ago

Triage Stage: UnreviewedDesign decision needed

comment:3 by Jacob, 17 years ago

Needs documentation: set
Patch needs improvement: set
Summary: Last and nth template filtersAdd a "last" template filter
Triage Stage: Design decision neededAccepted

nth isn't needed: you can do {{ list.14 }} (or whatever) instead. last, though, sounds like a good idea.

by Aaron C. de Bruyn, 17 years ago

Attachment: filter-last.diff added

by Aaron C. de Bruyn, 17 years ago

Attachment: filter-last.2.diff added

comment:4 by Aaron C. de Bruyn, 17 years ago

Changed first.is_safe = False to patch as well.

comment:5 by Chris Beaven, 17 years ago

Needs documentation: unset
Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

Thanks darkpixel.

(We discussed changing is_safe to false in IRC, first and last don't guarantee a safe string out, eg first(mark_safe('&')))

comment:6 by Malcolm Tredinnick, 17 years ago

I can't think of any example where last() is going to return an unsafe result for a safe string. The first() change is correct, though.

comment:7 by Chris Beaven, 17 years ago

perhaps last(mark_safe('<a>') -- just because '<a>' is safe doesn't mean that '>' should be considered safe, does it? (but this is probably misuse of the filter anyway, it's obviously meant primarily for lists)

comment:8 by Malcolm Tredinnick, 17 years ago

(In [6997]) The "first" filter can return an unsafe string for safe input ( {{"&lt;"|first}} ), so change is_safe to False. Refs #5567.

comment:9 by Malcolm Tredinnick, 17 years ago

Resolution: fixed
Status: newclosed

(In [6998]) Fixed #5567 -- Added a "last" filter. Based on a patch from darkpixel.

comment:10 by Malcolm Tredinnick, 17 years ago

If "<a>" is marked as safe, then the user intended for the ">" to be displayed literally. There's no problem there. The ">" was already marked appropriately and its semantics don't change just because it doesn't have the "<a" prefix any longer (c.f. "&lt;" where the semantics change when you strip off the trailing "lt;", as you noted).

in reply to:  3 comment:11 by Antti Kaihola, 14 years ago

Replying to jacob:

nth isn't needed: you can do {{ list.14 }} (or whatever) instead. last, though, sounds like a good idea.

What about {{ list|nth:offset }} where offset is an integer?

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