Django

Code

Ticket #5567 (closed: fixed)

Opened 10 months ago

Last modified 7 months ago

Add a "last" template filter

Reported by: ChrisH Assigned to: nobody
Milestone: Component: Template system
Version: SVN Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

last_and_nth_filters.diff (1.1 kB) - added by ChrisH on 09/21/07 16:36:50.
filter-last.diff (0.9 kB) - added by darkpixel on 12/09/07 22:17:59.
filter-last.2.diff (1.3 kB) - added by darkpixel on 12/09/07 22:27:13.

Change History

09/21/07 16:36:50 changed by ChrisH

  • attachment last_and_nth_filters.diff added.

09/23/07 04:06:29 changed by ubernostrum

  • needs_better_patch changed.
  • description changed.
  • needs_tests changed.
  • needs_docs changed.

(cleaning up formatting in description)

09/23/07 04:06:56 changed by ubernostrum

  • stage changed from Unreviewed to Design decision needed.

12/04/07 08:53:12 changed by jacob

  • needs_better_patch set to 1.
  • stage changed from Design decision needed to Accepted.
  • summary changed from Last and nth template filters to Add a "last" template filter.
  • needs_docs set to 1.

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

12/09/07 22:17:59 changed by darkpixel

  • attachment filter-last.diff added.

12/09/07 22:27:13 changed by darkpixel

  • attachment filter-last.2.diff added.

12/09/07 22:28:00 changed by darkpixel

Changed first.is_safe = False to patch as well.

12/09/07 22:30:50 changed by SmileyChris

  • needs_better_patch deleted.
  • stage changed from Accepted to Ready for checkin.
  • needs_docs deleted.

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('&')))

01/05/08 21:38:01 changed 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.

01/05/08 21:50:06 changed 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)

01/05/08 21:53:04 changed 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.

01/05/08 21:53:33 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

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

01/05/08 21:57:57 changed 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).


Add/Change #5567 (Add a "last" template filter)




Change Properties
Action