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'},