templatetags: add |split (#33042)
This commit is contained in:
parent
ede3575fb2
commit
1d6ec5f7e0
|
@ -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 = []
|
||||
|
|
|
@ -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'},
|
||||
|
|
Loading…
Reference in New Issue