summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCorentin Séchet <csechet@entrouvert.com>2022-07-04 19:25:47 (GMT)
committerCorentin Séchet <csechet@entrouvert.com>2022-07-04 19:25:47 (GMT)
commitc71350052e7c2d33870778ce2b2c8b8313033055 (patch)
tree305d7f08d3600e9df8959ee509ba89d227a39930
parentfae07a449eb4eccba2ffab3c082534f3a4eda0f7 (diff)
downloadpublik-django-templatetags-c71350052e7c2d33870778ce2b2c8b8313033055.zip
publik-django-templatetags-c71350052e7c2d33870778ce2b2c8b8313033055.tar.gz
publik-django-templatetags-c71350052e7c2d33870778ce2b2c8b8313033055.tar.bz2
publik: add |last template filter (#66955)
-rw-r--r--publik_django_templatetags/publik/templatetags/publik.py8
-rw-r--r--tests/test_publik.py16
2 files changed, 24 insertions, 0 deletions
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) == ''