diff --git a/combo/public/templatetags/combo.py b/combo/public/templatetags/combo.py index 5d3583b0..b9f6ac61 100644 --- a/combo/public/templatetags/combo.py +++ b/combo/public/templatetags/combo.py @@ -28,6 +28,7 @@ from django.template import VariableDoesNotExist from django.template.base import TOKEN_BLOCK, TOKEN_VAR, TOKEN_COMMENT from django.template.defaultfilters import stringfilter from django.utils import dateparse +from django.utils.encoding import force_text from combo.data.models import Page, Placeholder from combo.public.menu import get_menu_context @@ -264,3 +265,7 @@ def name_id(user): @register.simple_tag def get_page(page_slug): return Page.objects.get(slug=page_slug) + +@register.filter +def startswith(string, substring): + return string and force_text(string).startswith(force_text(substring)) diff --git a/tests/test_public_templatetags.py b/tests/test_public_templatetags.py index ead51e40..d5efd57c 100644 --- a/tests/test_public_templatetags.py +++ b/tests/test_public_templatetags.py @@ -192,3 +192,12 @@ def test_asset_template_tags(): page.save() t = Template('''{% load assets %}{% asset_url page.picture "collectivity:banner" size="200x200" %}''') assert t.render(Context({'page': page})) == '/media/page-pictures/test2.svg' + +def test_startswith(): + t = Template('{% if foo|startswith:"bar" %}ok{% endif %}') + context = Context({'foo': None}) + assert t.render(context) == '' + context = Context({'foo': 'xx'}) + assert t.render(context) == '' + context = Context({'foo': 'bar'}) + assert t.render(context) == 'ok'