From c71350052e7c2d33870778ce2b2c8b8313033055 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Corentin=20S=C3=A9chet?= Date: Mon, 4 Jul 2022 21:25:47 +0200 Subject: [PATCH] publik: add |last template filter (#66955) --- .../publik/templatetags/publik.py | 8 ++++++++ tests/test_publik.py | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) 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) == ''