From 7c0116f0dc20171791ca8064c6331f3166b9de67 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sat, 12 Apr 2008 00:06:19 +0200 Subject: [PATCH] fixed include --HG-- branch : trunk --- jinja2/parser.py | 11 +++++++---- jinja2/runtime.py | 17 +++++++++++++---- test.py | 7 ++++--- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/jinja2/parser.py b/jinja2/parser.py index f1195c4..400b9be 100644 --- a/jinja2/parser.py +++ b/jinja2/parser.py @@ -161,12 +161,15 @@ class Parser(object): 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 diff --git a/jinja2/runtime.py b/jinja2/runtime.py index fcd537d..1bc196e 100644 --- a/jinja2/runtime.py +++ b/jinja2/runtime.py @@ -96,10 +96,12 @@ class IncludedTemplate(object): """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] @@ -107,6 +109,12 @@ class IncludedTemplate(object): def __unicode__(self): return self._context + def __repr__(self): + return '<%s %r>' % ( + self.__class__.__name__, + self._filename + ) + class LoopContextBase(object): """Helper for extended iteration.""" @@ -230,7 +238,8 @@ class Undefined(object): 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): diff --git a/test.py b/test.py index 45a5e65..b62c84f 100644 --- a/test.py +++ b/test.py @@ -4,16 +4,17 @@ from jinja2.loaders import DictLoader 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'''\ -Foo +{{ title }} {% block body %}{% endblock %} ''', 'helpers.html': u'''\ -- 2.26.2