templatetags: add |split (#33042)

This commit is contained in:
Frédéric Péters 2019-05-13 12:56:00 +02:00
parent ede3575fb2
commit 1d6ec5f7e0
2 changed files with 10 additions and 0 deletions

View File

@ -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 = []

View File

@ -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}}<br>{% endfor %}')
assert t.render(Context({'plop': 'ab cd ef'})) == 'ab<br>cd<br>ef<br>'
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'},