diff --git a/publik_django_templatetags/publik/templatetags/publik.py b/publik_django_templatetags/publik/templatetags/publik.py index 8a03e34..d56990f 100644 --- a/publik_django_templatetags/publik/templatetags/publik.py +++ b/publik_django_templatetags/publik/templatetags/publik.py @@ -54,3 +54,11 @@ def first(value): return defaultfilters.first(value) except TypeError: return '' + + +@register.filter +def last(value): + try: + return defaultfilters.last(value) + except TypeError: + return '' diff --git a/tests/test_publik.py b/tests/test_publik.py index d83d244..c3dd2e7 100644 --- a/tests/test_publik.py +++ b/tests/test_publik.py @@ -68,3 +68,19 @@ def test_first(): context = Context({'foo': None}) assert t.render(context) == '' + + +def test_last(): + t = Template('{{ foo|last }}') + + context = Context({'foo': ['foo']}) + assert t.render(context) == 'foo' + + context = Context({'foo': 'foo'}) + assert t.render(context) == 'o' + + context = Context({'foo': ''}) + assert t.render(context) == '' + + context = Context({'foo': None}) + assert t.render(context) == ''