templatetags: add 'endswith' template filter (#43558)

This commit is contained in:
Nicolas Roche 2020-06-03 10:09:29 +02:00
parent df6ca9ca4b
commit 2892f355d4
2 changed files with 14 additions and 0 deletions

View File

@ -343,6 +343,12 @@ def get_page(page_slug):
def startswith(string, substring):
return string and force_text(string).startswith(force_text(substring))
@register.filter
def endswith(string, substring):
return string and force_text(string).endswith(force_text(substring))
def parse_float(value):
if isinstance(value, six.string_types):
# replace , by . for French users comfort

View File

@ -241,6 +241,14 @@ def test_startswith():
context = Context({'foo': 'bar'})
assert t.render(context) == 'ok'
def test_endswith_templatetag():
tmpl = Template('{% if foo|endswith:"bar" %}ok{% endif %}')
assert tmpl.render(Context({'foo': None})) == ''
assert tmpl.render(Context({'foo': 'bar-baz'})) == ''
assert tmpl.render(Context({'foo': 'baz-bar'})) == 'ok'
def test_datetime_templatetags():
tmpl = Template('{{ plop|datetime }}')
assert tmpl.render(Context({'plop': '2017-12-21 10:32'})) == 'Dec. 21, 2017, 10:32 a.m.'