publik: add |last template filter (#66955)

This commit is contained in:
Corentin Sechet 2022-07-04 21:25:47 +02:00
parent fae07a449e
commit c71350052e
2 changed files with 24 additions and 0 deletions

View File

@ -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 ''

View File

@ -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) == ''