#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 )
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)
Change History (14)
by , 17 years ago
Attachment: | last_and_nth_filters.diff added |
---|
comment:1 by , 17 years ago
Description: | modified (diff) |
---|
comment:2 by , 17 years ago
Triage Stage: | Unreviewed → Design decision needed |
---|
follow-up: 11 comment:3 by , 17 years ago
Needs documentation: | set |
---|---|
Patch needs improvement: | set |
Summary: | Last and nth template filters → Add a "last" template filter |
Triage Stage: | Design decision needed → Accepted |
nth
isn't needed: you can do {{ list.14
}} (or whatever) instead. last
, though, sounds like a good idea.
by , 17 years ago
Attachment: | filter-last.diff added |
---|
by , 17 years ago
Attachment: | filter-last.2.diff added |
---|
comment:5 by , 17 years ago
Needs documentation: | unset |
---|---|
Patch needs improvement: | unset |
Triage Stage: | Accepted → 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 by , 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 , 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 , 17 years ago
comment:9 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:10 by , 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. "<" where the semantics change when you strip off the trailing "lt;", as you noted).
comment:11 by , 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?
(cleaning up formatting in description)