Opened 16 years ago

Closed 15 years ago

Last modified 12 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. 16 years ago.
filter-last.diff (886 bytes) - added by Aaron C. de Bruyn 15 years ago.
filter-last.2.diff (1.3 KB) - added by Aaron C. de Bruyn 15 years ago.

Download all attachments as: .zip

Change History (14)

Changed 16 years ago by Chris H.

Attachment: last_and_nth_filters.diff added

comment:1 Changed 16 years ago by James Bennett

Description: modified (diff)

(cleaning up formatting in description)

comment:2 Changed 16 years ago by James Bennett

Triage Stage: UnreviewedDesign decision needed

comment:3 Changed 15 years ago by Jacob

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.

Changed 15 years ago by Aaron C. de Bruyn

Attachment: filter-last.diff added

Changed 15 years ago by Aaron C. de Bruyn

Attachment: filter-last.2.diff added

comment:4 Changed 15 years ago by Aaron C. de Bruyn

Changed first.is_safe = False to patch as well.

comment:5 Changed 15 years ago by Chris Beaven

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 Changed 15 years ago by Malcolm Tredinnick

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 Changed 15 years ago by Chris Beaven

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 Changed 15 years ago by Malcolm Tredinnick

(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 Changed 15 years ago by Malcolm Tredinnick

Resolution: fixed
Status: newclosed

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

comment:10 Changed 15 years ago by Malcolm Tredinnick

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).

comment:11 in reply to:  3 Changed 12 years ago by Antti Kaihola

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