From cf382652f51cc90d5db538d29b6331c96e70f03f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Thu, 30 Aug 2018 17:33:47 +0200 Subject: [PATCH] misc: add startswith templatetag (#25972) --- tests/test_templates.py | 6 ++++++ wcs/qommon/templatetags/qommon.py | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/tests/test_templates.py b/tests/test_templates.py index 00ccc1dd..21245566 100644 --- a/tests/test_templates.py +++ b/tests/test_templates.py @@ -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') diff --git a/wcs/qommon/templatetags/qommon.py b/wcs/qommon/templatetags/qommon.py index 8e24078e..64a6c958 100644 --- a/wcs/qommon/templatetags/qommon.py +++ b/wcs/qommon/templatetags/qommon.py @@ -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: -- 2.18.0