Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #5567 -- Added a "last" filter. Based on a patch from darkpixel.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6998 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a7da12d32090e887b72093b7e8f2870a8d7cf3a7 1 parent 1f6bc7f
@malcolmt malcolmt authored
View
9 django/template/defaultfilters.py
@@ -449,6 +449,14 @@ def join(value, arg):
return data
join.is_safe = True
+def last(value):
+ "Returns the last item in a list"
+ try:
+ return value[-1]
+ except IndexError:
+ return u''
+last.is_safe = True
+
def length(value):
"""Returns the length of the value - useful for lists."""
return len(value)
@@ -800,6 +808,7 @@ def pprint(value):
register.filter(get_digit)
register.filter(iriencode)
register.filter(join)
+register.filter(last)
register.filter(length)
register.filter(length_is)
register.filter(linebreaks)
View
5 docs/templates.txt
@@ -1403,6 +1403,11 @@ join
Joins a list with a string, like Python's ``str.join(list)``.
+last
+~~~~
+
+Returns the last item in a list.
+
length
~~~~~~
View
3  tests/regressiontests/templates/filters.py
@@ -179,6 +179,9 @@ def get_filter_tests():
'filter-first01': ('{{ a|first }} {{ b|first }}', {"a": ["a&b", "x"], "b": [mark_safe("a&b"), "x"]}, "a&b a&b"),
'filter-first02': ('{% autoescape off %}{{ a|first }} {{ b|first }}{% endautoescape %}', {"a": ["a&b", "x"], "b": [mark_safe("a&b"), "x"]}, "a&b a&b"),
+ 'filter-last01': ('{{ a|last }} {{ b|last }}', {"a": ["x", "a&b"], "b": ["x", mark_safe("a&b")]}, "a&b a&b"),
+ 'filter-last02': ('{% autoescape off %}{{ a|last }} {{ b|last }}{% endautoescape %}', {"a": ["x", "a&b"], "b": ["x", mark_safe("a&b")]}, "a&b a&b"),
+
'filter-random01': ('{{ a|random }} {{ b|random }}', {"a": ["a&b", "a&b"], "b": [mark_safe("a&b"), mark_safe("a&b")]}, "a&b a&b"),
'filter-random02': ('{% autoescape off %}{{ a|random }} {{ b|random }}{% endautoescape %}', {"a": ["a&b", "a&b"], "b": [mark_safe("a&b"), mark_safe("a&b")]}, "a&b a&b"),
Please sign in to comment.
Something went wrong with that request. Please try again.