From 73fe1c852f618e00fecf432d4c359928f0a5b7ba 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:39:08 +0200 Subject: [PATCH] misc: add split templatetag (#25973) --- tests/test_templates.py | 11 +++++++++++ wcs/qommon/templatetags/qommon.py | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/tests/test_templates.py b/tests/test_templates.py index 21245566..2dd043cf 100644 --- a/tests/test_templates.py +++ b/tests/test_templates.py @@ -73,6 +73,17 @@ def test_startswith_templatetag(): assert tmpl.render({'foo': 'bar-baz'}) == 'hello' assert tmpl.render({'foo': 'baz-bar'}) == '' +def test_split_templatetag(): + tmpl = Template('{{ foo|split|last }}') + assert tmpl.render() == '' + assert tmpl.render({'foo': 'bar baz'}) == 'baz' + assert tmpl.render({'foo': 'baz-bar'}) == 'baz-bar' + + tmpl = Template('{{ foo|split:"-"|last }}') + assert tmpl.render() == '' + assert tmpl.render({'foo': 'bar-baz'}) == 'baz' + assert tmpl.render({'foo': 'baz-bar'}) == '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 64a6c958..4919db11 100644 --- a/wcs/qommon/templatetags/qommon.py +++ b/wcs/qommon/templatetags/qommon.py @@ -31,6 +31,10 @@ def get(mapping, key): def startswith(string, substring): return string and string.startswith(substring) +@register.filter +def split(string, separator=' '): + return (string or '').split(separator) + @register.filter def parse_date(date_string): try: -- 2.18.0