Ticket #5567: last_and_nth_filters.diff

File last_and_nth_filters.diff, 1.1 KB (added by Chris H., 17 years ago)
  • django/template/defaultfilters.py

     
    320320    except IndexError:
    321321        return u''
    322322
     323def last(value):
     324    "Returns the last item in a list"
     325    try:
     326        return value[-1]
     327    except IndexError:
     328        return u''
     329   
     330def nth(value, arg):
     331    "Returns the nth item in a list"
     332    try:
     333        return value[int(arg)]
     334    except IndexError:
     335        return u''
     336
    323337def join(value, arg):
    324338    "Joins a list with a string, like Python's ``str.join(list)``"
    325339    try:
  • tests/regressiontests/defaultfilters/tests.py

     
    490490u'123'
    491491>>> striptags(123)
    492492u'123'
    493 
     493>>> last([0, 1, 2, 3, 4])
     4944
     495>>> last(u'Hello')
     496u'o'
     497>>> nth(['a', 'b', 'c', 'd'], 1)
     498'b'
     499>>> nth(u'abcd', 1)
     500u'b'
    494501"""
    495502
    496503from django.template.defaultfilters import *
Back to Top