raise TemplateAssertionError('It\'s not possible to set and '
'access variables derived from '
'an outer scope! (affects: %s' %
- vars, node.lineno, self.name)
+ vars, node.lineno, self.filename)
# remove variables from a closure from the frame's undeclared
# identifiers.
if block.name in self.blocks:
raise TemplateAssertionError('block %r defined twice' %
block.name, block.lineno,
- self.name)
+ self.filename)
self.blocks[block.name] = block
# find all imports and import them
self.writeline('import %s as %s' % (imp, alias))
# add the load name
- self.writeline('name = %r' % self.name)
+ self.writeline('name = %r' % self.filename)
# generate the root render function.
self.writeline('def root(context, environment=environment):', extra=1)
if not frame.toplevel:
raise TemplateAssertionError('cannot use extend from a non '
'top-level scope', node.lineno,
- self.name)
+ self.filename)
# if the number of extends statements in general is zero so
# far, we don't have to add a check if something extended
def __iter__(self):
return self
- def __call__(self, iterable):
+ def loop(self, iterable):
if self._recurse is None:
raise TypeError('Tried to call non recursive loop. Maybe you '
- 'forgot the "recursive" keyword.')
+ "forgot the 'recursive' modifier.")
return self._recurse(iterable, self._recurse)
+ # a nifty trick to enhance the error message if someone tried to call
+ # the the loop without or with too many arguments.
+ __call__ = loop; del loop
+
def next(self):
self.index0 += 1
return self._next(), self
SCOPE = '''{% for item in seq %}{% endfor %}{{ item }}'''
VARLEN = '''{% for item in iter %}{{ item }}{% endfor %}'''
NONITER = '''{% for item in none %}...{% endfor %}'''
+RECURSIVE = '''{% for item in seq recursive -%}
+ [{{ item.a }}{% if item.b %}[{{ loop(item.b) }}]{% endif %}]
+{%- endfor %}'''
def test_simple(env):
def test_noniter(env):
tmpl = env.from_string(NONITER)
raises(TypeError, tmpl.render)
+
+
+def test_recursive(env):
+ tmpl = env.from_string(RECURSIVE)
+ assert tmpl.render(seq=[
+ dict(a=1, b=[dict(a=1), dict(a=2)]),
+ dict(a=2, b=[dict(a=1), dict(a=2)]),
+ dict(a=3, b=[dict(a='a')])
+ ]) == '[1[[1][2]]][2[[1][2]]][3[[a]]]'