Commit dd9184bb by Lucas Beyer

Allows to use generators just like lists/tuples.

This is done by treating any object with __iter__ methods the way only
'list' and 'tuple' objects were treated before. Except dictionaries,
they have an __iter__ method too, but that's not what we want.

All 291 tests aswell as the new one pass.
parent 32adefb9
...@@ -168,7 +168,7 @@ class RenderEngine(object): ...@@ -168,7 +168,7 @@ class RenderEngine(object):
template = data(template) template = data(template)
parsed_template = self._parse(template, delimiters=delims) parsed_template = self._parse(template, delimiters=delims)
data = [ data ] data = [ data ]
elif type(data) not in [list, tuple]: elif not hasattr(data, '__iter__') or isinstance(data, dict):
data = [ data ] data = [ data ]
parts = [] parts = []
......
...@@ -376,6 +376,22 @@ class RenderTests(unittest.TestCase): ...@@ -376,6 +376,22 @@ class RenderTests(unittest.TestCase):
context = {'test': (lambda text: 'Hi %s' % text)} context = {'test': (lambda text: 'Hi %s' % text)}
self._assert_render('Hi Mom', template, context) self._assert_render('Hi Mom', template, context)
def test_section__generator(self):
"""
Generator expressions should behave the same way as lists do.
This actually tests for everything which has an __iter__ method.
"""
template = '{{#gen}}{{.}}{{/gen}}'
context = {'gen': (i for i in range(5))}
self._assert_render('01234', template, context)
context = {'gen': xrange(5)}
self._assert_render('01234', template, context)
d = {'1': 1, '2': 2, 'abc': 3}
context = {'gen': d.iterkeys()}
self._assert_render(''.join(d.keys()), template, context)
def test_section__lambda__tag_in_output(self): def test_section__lambda__tag_in_output(self):
""" """
Check that callable output is treated as a template string (issue #46). Check that callable output is treated as a template string (issue #46).
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment