Opened 8 years ago

Closed 8 years ago

Last modified 5 years ago

#5567 closed (fixed)

Add a "last" template filter

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

Description (last modified by ubernostrum)

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 ChrisH 8 years ago.
filter-last.diff (886 bytes) - added by darkpixel 8 years ago.
filter-last.2.diff (1.3 KB) - added by darkpixel 8 years ago.

Download all attachments as: .zip

Change History (14)

Changed 8 years ago by ChrisH

comment:1 Changed 8 years ago by ubernostrum

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

(cleaning up formatting in description)

comment:2 Changed 8 years ago by ubernostrum

  • Triage Stage changed from Unreviewed to Design decision needed

comment:3 follow-up: Changed 8 years ago by jacob

  • Needs documentation set
  • Patch needs improvement set
  • Summary changed from Last and nth template filters to Add a "last" template filter
  • Triage Stage changed from Design decision needed to Accepted

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

Changed 8 years ago by darkpixel

Changed 8 years ago by darkpixel

comment:4 Changed 8 years ago by darkpixel

Changed first.is_safe = False to patch as well.

comment:5 Changed 8 years ago by SmileyChris

  • Needs documentation unset
  • Patch needs improvement unset
  • Triage Stage changed from Accepted to Ready 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 8 years ago by mtredinnick

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 8 years ago by SmileyChris

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 8 years ago by mtredinnick

(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 8 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from new to closed

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

comment:10 Changed 8 years ago by mtredinnick

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 5 years ago by akaihola

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