[svn] requirements (macros and set directives) can be outside of renderable blocks too
authorArmin Ronacher <armin.ronacher@active-4.com>
Tue, 6 Mar 2007 11:28:47 +0000 (12:28 +0100)
committerArmin Ronacher <armin.ronacher@active-4.com>
Tue, 6 Mar 2007 11:28:47 +0000 (12:28 +0100)
--HG--
branch : trunk

jinja/parser.py
jinja/translators/python.py
jinja/utils.py
tests/templates/index.html

index 98553bcf6ebc55ce5699143aa8256218ee5a388f..33c8aed9df1e1a343dd56009582daca63bcf29f3 100644 (file)
@@ -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):
         """
index ce8f1dff4adae965d3572aef64ab8cfb9577b702..a4238209192f804ea381b6aada8b9ec264d24b3b 100644 (file)
@@ -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
         ))
index 7cbf77eca57ba88c84c7e904226e210116c6d7d6..6441921e96e1091b80d1fb4f9dd36de179e16b57 100644 (file)
@@ -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
index 1f8cb7af8e0b01ebade760423165732241e92a82..1026da63cf5f79eec872bf1d7c0e87bc47d1d387 100644 (file)
@@ -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.<br />
+  {{ greet('John Doe') }}
 {% endblock %}