From: Armin Ronacher Date: Tue, 6 Mar 2007 11:28:47 +0000 (+0100) Subject: [svn] requirements (macros and set directives) can be outside of renderable blocks too X-Git-Tag: 2.0rc1~458 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=4fd2ca591d6ba17052d3eb8548ee18b81ccd78a4;p=jinja2.git [svn] requirements (macros and set directives) can be outside of renderable blocks too --HG-- branch : trunk --- diff --git a/jinja/parser.py b/jinja/parser.py index 98553bc..33c8aed 100644 --- a/jinja/parser.py +++ b/jinja/parser.py @@ -157,7 +157,7 @@ class Parser(object): except (StopIteration, ValueError): raise TemplateSyntaxError('invalid syntax for set', lineno) ast = self.parse_python(lineno, gen, '(%s)') - return nodes.Set(lineno, name[2], ast.expr) + return nodes.Set(lineno, str(name[2]), ast.expr) def handle_filter_directive(self, lineno, gen): """ diff --git a/jinja/translators/python.py b/jinja/translators/python.py index ce8f1df..a423820 100644 --- a/jinja/translators/python.py +++ b/jinja/translators/python.py @@ -209,18 +209,35 @@ class PythonTranslator(Translator): # update the blocks there. Once this is done we drop the current # template in favor of the new one. Do that until we found the # root template. + requirements_todo = [] while node.extends is not None: + if node not in requirements_todo: + requirements_todo.append(node) + tmpl = self.environment.loader.parse(node.extends.template, node.filename) + # handle block inheritance for block in tmpl.blocks.itervalues(): if block.name in node.blocks: block.replace(node.blocks[block.name]) node = tmpl + if tmpl not in requirements_todo: + requirements_todo.append(node) + + # look up requirements + requirements = [] + for req in requirements_todo: + for n in req: + if n.__class__ in (nodes.Set, nodes.Macro): + requirements.append(n) + + # bootstrapping code lines = [ 'from __future__ import division\n' 'from jinja.datastructure import Undefined, LoopContext, CycleContext\n\n' 'def generate(context, write):\n' + ' # BOOTSTRAPPING CODE\n' ' environment = context.environment\n' ' get_attribute = environment.get_attribute\n' ' perform_test = environment.perform_test\n' @@ -230,8 +247,18 @@ class PythonTranslator(Translator): ' finish_var = environment.finish_var' ] self.indention = 1 + + # we have requirements? add them here. + if requirements: + lines.append(self.indent('# REQUIREMENTS')) + for n in requirements: + lines.append(self.handle_node(n)) + lines.append(self.indent('# END OF REQUIREMENTS')) + + # the template body rv = self.handle_node_list(node) + # add translation helpers if required if self.require_translations: lines.append( ' translator = context.get_translator()\n' @@ -433,7 +460,7 @@ class PythonTranslator(Translator): """ rv = self.handle_node(node.body) if not rv: - return self.indent('# EMPTY BLOCK (%r:%s)' % ( + return self.indent('# EMPTY BLOCK FROM %r, LINE %s' % ( node.filename or '?', node.lineno )) @@ -441,7 +468,7 @@ class PythonTranslator(Translator): buf = [] write = lambda x: buf.append(self.indent(x)) - write('# BLOCK (%r:%s)' % ( + write('# BLOCK FROM %r, LINE %s' % ( node.filename or '?', node.lineno )) diff --git a/jinja/utils.py b/jinja/utils.py index 7cbf77e..6441921 100644 --- a/jinja/utils.py +++ b/jinja/utils.py @@ -36,7 +36,6 @@ def escape(x, attribute=False): _escape_pairs[m.group()], unicode(x))) - def find_translations(environment, source): """ Find all translatable strings in a template and yield diff --git a/tests/templates/index.html b/tests/templates/index.html index 1f8cb7a..1026da6 100644 --- a/tests/templates/index.html +++ b/tests/templates/index.html @@ -1,5 +1,13 @@ {% extends "layout.html" %} + +{% macro say_hello name %} +Hello {{ name }}!. +{% endmacro %} + +{% set greet = say_hello %} + {% block page_title %}Index{% endblock %} {% block body %} - This is the index page. + This is the index page.
+ {{ greet('John Doe') }} {% endblock %}