fixed include
authorArmin Ronacher <armin.ronacher@active-4.com>
Fri, 11 Apr 2008 22:06:19 +0000 (00:06 +0200)
committerArmin Ronacher <armin.ronacher@active-4.com>
Fri, 11 Apr 2008 22:06:19 +0000 (00:06 +0200)
--HG--
branch : trunk

jinja2/parser.py
jinja2/runtime.py
test.py

index f1195c4424c997802d18ac82b4b57f33939da91a..400b9be35f06c76437a07598e681831e4f987839 100644 (file)
@@ -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
index fcd537d9c4dfb338380ae4e3532996872bee044a..1bc196e4e9c8252f6de35140bd923e364efac270 100644 (file)
@@ -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 45a5e6522893748f85613a31f5d32d072d94dc8b..b62c84fb5ed40a743f24dd6c9461c727dddac1bd 100644 (file)
--- 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'''\
 <!doctype html>
-<title>Foo</title>
+<title>{{ title }}</title>
 {% block body %}{% endblock %}
 ''',
 'helpers.html': u'''\