From 771c7505c5e0203a29a8d48fa939898d7ce8e9c8 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sun, 18 May 2008 23:14:14 +0200 Subject: [PATCH] some more cleaning up --HG-- branch : trunk --- jinja2/compiler.py | 6 +++--- jinja2/environment.py | 31 ++++++++++++++----------------- jinja2/runtime.py | 2 ++ 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/jinja2/compiler.py b/jinja2/compiler.py index 2423787..8dd31df 100644 --- a/jinja2/compiler.py +++ b/jinja2/compiler.py @@ -642,7 +642,7 @@ class CodeGenerator(NodeVisitor): self.writeline('if parent_template is not None:') self.indent() self.writeline('for event in parent_template.' - 'root_render_func(context):') + '_root_render_func(context):') self.indent() self.writeline('yield event') self.outdent(2 + (not self.has_known_extends)) @@ -750,12 +750,12 @@ class CodeGenerator(NodeVisitor): self.writeline('template = environment.get_template(', node) self.visit(node.template, frame) self.write(', %r)' % self.name) - self.writeline('for event in template.root_render_func(' + self.writeline('for event in template._root_render_func(' 'template.new_context(context.parent, True)):') else: self.writeline('for event in environment.get_template(', node) self.visit(node.template, frame) - self.write(', %r).module._TemplateModule__body_stream:' % + self.write(', %r).module._body_stream:' % self.name) self.indent() if frame.buffer is None: diff --git a/jinja2/environment.py b/jinja2/environment.py index 5497397..37ebdc6 100644 --- a/jinja2/environment.py +++ b/jinja2/environment.py @@ -464,11 +464,14 @@ class Template(object): } exec code in namespace t.environment = environment + t.globals = globals t.name = namespace['name'] t.filename = code.co_filename - t.root_render_func = namespace['root'] t.blocks = namespace['blocks'] - t.globals = globals + + # render function and module + t._root_render_func = namespace['root'] + t._module = None # debug and loader helpers t._debug_info = namespace['debug_info'] @@ -486,8 +489,9 @@ class Template(object): This will return the rendered template as unicode string. """ + vars = dict(*args, **kwargs) try: - return concat(self._generate(*args, **kwargs)) + return concat(self._root_render_func(self.new_context(vars))) except: from jinja2.debug import translate_exception exc_type, exc_value, tb = translate_exception(sys.exc_info()) @@ -507,18 +511,15 @@ class Template(object): It accepts the same arguments as :meth:`render`. """ + vars = dict(*args, **kwargs) try: - for item in self._generate(*args, **kwargs): - yield item + for event in self._root_render_func(self.new_context(vars)): + yield event except: from jinja2.debug import translate_exception exc_type, exc_value, tb = translate_exception(sys.exc_info()) raise exc_type, exc_value, tb - def _generate(self, *args, **kwargs): - """Creates a new context and generates the template.""" - return self.root_render_func(self.new_context(dict(*args, **kwargs))) - def new_context(self, vars=None, shared=False): """Create a new template context for this template. The vars provided will be passed to the template. Per default the globals @@ -556,7 +557,7 @@ class Template(object): >>> t.module.foo() u'42' """ - if hasattr(self, '_module'): + if self._module is not None: return self._module self._module = rv = self.make_module() return rv @@ -598,16 +599,12 @@ class TemplateModule(object): """ def __init__(self, template, context): - # don't alter this attribute unless you change it in the - # compiler too. The Include without context passing directly - # uses the mangled name. The reason why we use a mangled one - # is to avoid name clashes with macros with those names. - self.__body_stream = list(template.root_render_func(context)) + self._body_stream = list(template._root_render_func(context)) self.__dict__.update(context.get_exported()) self.__name__ = template.name - __html__ = lambda x: Markup(concat(x.__body_stream)) - __unicode__ = lambda x: unicode(concat(x.__body_stream)) + __html__ = lambda x: Markup(concat(x._body_stream)) + __unicode__ = lambda x: unicode(concat(x._body_stream)) def __str__(self): return unicode(self).encode('utf-8') diff --git a/jinja2/runtime.py b/jinja2/runtime.py index 9a61040..085120b 100644 --- a/jinja2/runtime.py +++ b/jinja2/runtime.py @@ -56,6 +56,8 @@ class Context(object): method that doesn't fail with a `KeyError` but returns an :class:`Undefined` object for missing variables. """ + __slots__ = ('parent', 'vars', 'environment', 'exported_vars', 'name', + 'blocks') def __init__(self, environment, parent, name, blocks): self.parent = parent -- 2.26.2