misc: add startswith templatetag (#25972)

This commit is contained in:
Frédéric Péters 2018-08-30 17:33:47 +02:00
parent 8ce0d0b7de
commit 6dcb7fe9d7
2 changed files with 10 additions and 0 deletions

View File

@ -67,6 +67,12 @@ def test_template_templatetag():
tmpl = Template('{% load i18n %}{% trans "hello" %}')
assert tmpl.render() == 'hello'
def test_startswith_templatetag():
tmpl = Template('{% if foo|startswith:"bar" %}hello{% endif %}')
assert tmpl.render() == ''
assert tmpl.render({'foo': 'bar-baz'}) == 'hello'
assert tmpl.render({'foo': 'baz-bar'}) == ''
def test_template_encoding():
# django
tmpl = Template('{{ foo }} à vélo')

View File

@ -27,6 +27,10 @@ register = template.Library()
def get(mapping, key):
return mapping.get(key)
@register.filter
def startswith(string, substring):
return string and string.startswith(substring)
@register.filter
def parse_date(date_string):
try: