expr = self.parse_expression()
if self.stream.current.type is 'assign':
self.stream.next()
- node.target = self.stream.expect('name')
- # make sure that assignments to that name are allowed
- if not nodes.Name(node.target, 'store').can_assign():
+ if not isinstance(expr, nodes.Name):
+ raise TemplateSyntaxError('must assign imported template to '
+ 'variable or current scope',
+ expr.lineno, self.filename)
+ if not expr.can_assign():
raise TemplateSyntaxError('can\'t assign imported template '
- 'to %r' % node.target, expr.lineno,
+ 'to %r' % expr, expr.lineno,
self.filename)
+ node.target = expr.name
node.template = self.parse_expression()
else:
node.target = None
"""Represents an included template."""
def __init__(self, environment, context, template):
- root = environment.get_template(template).root_render_func
- gen = root(context, standalone=True)
- self._context = gen.next().get_exported()
+ template = environment.get_template(template)
+ gen = template.root_render_func(context, standalone=True)
+ context = gen.next()
+ self._filename = template.name
self._rendered_body = u''.join(gen)
+ self._context = context.get_exported()
def __getitem__(self, name):
return self._context[name]
def __unicode__(self):
return self._context
+ def __repr__(self):
+ return '<%s %r>' % (
+ self.__class__.__name__,
+ self._filename
+ )
+
class LoopContextBase(object):
"""Helper for extended iteration."""
def fail(self, *args, **kwargs):
raise TypeError(self._undefined_hint)
__getattr__ = __getitem__ = __add__ = __mul__ = __div__ = \
- __realdiv__ = __floordiv__ = __mod__ = __pos__ = __neg__ = fail
+ __realdiv__ = __floordiv__ = __mod__ = __pos__ = __neg__ = \
+ __call__ = fail
del fail
def __unicode__(self):
env = Environment(loader=DictLoader({
'child.html': u'''\
{% extends master_layout or 'master.html' %}
-{% include 'helpers.html' %}
+{% include helpers = 'helpers.html' %}
{% macro get_the_answer() %}42{% endmacro %}
+{% title = 'Hello World' %}
{% block body %}
{{ get_the_answer() }}
- {{ conspirate() }}
+ {{ helpers.conspirate() }}
{% endblock %}
''',
'master.html': u'''\
<!doctype html>
-<title>Foo</title>
+<title>{{ title }}</title>
{% block body %}{% endblock %}
''',
'helpers.html': u'''\