From 1d6ec5f7e0e87a81e328af6986b934eaa71afa2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Mon, 13 May 2019 12:56:00 +0200 Subject: [PATCH] templatetags: add |split (#33042) --- combo/public/templatetags/combo.py | 4 ++++ tests/test_public_templatetags.py | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/combo/public/templatetags/combo.py b/combo/public/templatetags/combo.py index 41e8c5ab..67302296 100644 --- a/combo/public/templatetags/combo.py +++ b/combo/public/templatetags/combo.py @@ -220,6 +220,10 @@ def get(obj, key): except AttributeError: return None +@register.filter +def split(string, separator=' '): + return (string or '').split(separator) + @register.filter(name='get_group') def get_group(group_list, group_name): ret = [] diff --git a/tests/test_public_templatetags.py b/tests/test_public_templatetags.py index 70cacef8..f13b82b3 100644 --- a/tests/test_public_templatetags.py +++ b/tests/test_public_templatetags.py @@ -122,6 +122,12 @@ def test_get(): context = Context({'foo': {'foo-bar': 'hello'}, 'key': 'foo-bar'}) assert t.render(context) == 'hello' +def test_split(): + t = Template('{% load combo %}{% for x in plop|split %}{{x}}
{% endfor %}') + assert t.render(Context({'plop': 'ab cd ef'})) == 'ab
cd
ef
' + t = Template('{% load combo %}{% for x in plop|split:"|" %}{{x}} {% endfor %}') + assert t.render(Context({'plop': 'ab|cd|ef'})) == 'ab cd ef ' + def test_get_group(): context = Context({'cities': [ {'name': 'Mumbai', 'population': '19,000,000', 'country': 'India'},