From: Armin Ronacher Date: Fri, 25 Apr 2008 10:32:36 +0000 (+0200) Subject: `IncludedTemplate` uses getattr rather then getitem now X-Git-Tag: 2.0rc1~135 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=6ce170c496157ca994ceb90790067a6b845274a3;p=jinja2.git `IncludedTemplate` uses getattr rather then getitem now --HG-- branch : trunk --- diff --git a/jinja2/compiler.py b/jinja2/compiler.py index 8282bc6..542beed 100644 --- a/jinja2/compiler.py +++ b/jinja2/compiler.py @@ -793,7 +793,7 @@ class CodeGenerator(NodeVisitor): def visit_ExprStmt(self, node, frame): self.newline(node) - self.visit(node, frame) + self.visit(node.node, frame) def visit_Output(self, node, frame): # if we have a known extends statement, we don't output anything diff --git a/jinja2/environment.py b/jinja2/environment.py index a982e8e..239193e 100644 --- a/jinja2/environment.py +++ b/jinja2/environment.py @@ -15,7 +15,7 @@ from jinja2.optimizer import optimize from jinja2.compiler import generate from jinja2.runtime import Undefined, TemplateContext from jinja2.debug import translate_exception -from jinja2.utils import import_string, LRUCache +from jinja2.utils import import_string, LRUCache, Markup from jinja2.defaults import DEFAULT_FILTERS, DEFAULT_TESTS, DEFAULT_NAMESPACE @@ -348,7 +348,12 @@ class Template(object): self.name, self.blocks) def include(self, context=None): - """Include this template.""" + """Include this template. When passed a template context or dict + the template is evaluated in that context and an `IncludedTemplate` + object is returned. This object then exposes all the exported + variables as attributes and renders the contents of the template + when converted to unicode. + """ if context is None: context = self.new_context({}) elif isinstance(context, TemplateContext): @@ -390,13 +395,16 @@ class IncludedTemplate(object): """Represents an included template.""" def __init__(self, template, context): - self._template = template + body = Markup(u''.join(template.root_render_func(context))) + self.__dict__.update(context.get_exported()) self._name = template.name - self._rendered_body = u''.join(template.root_render_func(context)) - self._context = context.get_exported() + self._rendered_body = body + + __html__ = lambda x: x._rendered_body + __unicode__ = lambda x: unicode(x._rendered_body) - __getitem__ = lambda x, n: x._context[n] - __html__ = __unicode__ = lambda x: x._rendered_body + def __str__(self): + return unicode(self._rendered_body).encode('utf-8') def __repr__(self): return '<%s %r>' % (