[svn] requirements (macros and set directives) can be outside of renderable blocks too
Before adoption: this commit came in with the history the project adopted, so no landing records it.
--HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent 7c3be90 · Browse files · 4 files·+39 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+39 added−5 removed
- tests:+9 added−1 removed
- other:+30 added−4 removed
Provenance
- Commit
4fd2ca5- Tree
02a6c7f- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
7c3be90
4 files changed+39 added−5 removed
modified:jinja/translators/python.py+29 added-2 removed
207 unmodified lines208 # if there is a parent template we parse the parent template and209 # update the blocks there. Once this is done we drop the current210 # template in favor of the new one. Do that until we found the211 # root template.212+ requirements_todo = []213 while node.extends is not None:214+ if node not in requirements_todo:215+ requirements_todo.append(node)216+217 tmpl = self.environment.loader.parse(node.extends.template,218 node.filename)219+ # handle block inheritance220 for block in tmpl.blocks.itervalues():221 if block.name in node.blocks:222 block.replace(node.blocks[block.name])223 node = tmpl224 225+ if tmpl not in requirements_todo:226+ requirements_todo.append(node)227+228+ # look up requirements229+ requirements = []230+ for req in requirements_todo:231+ for n in req:232+ if n.__class__ in (nodes.Set, nodes.Macro):233+ requirements.append(n)234+235+ # bootstrapping code236 lines = [237 'from __future__ import division\n'238 'from jinja.datastructure import Undefined, LoopContext, CycleContext\n\n'239 'def generate(context, write):\n'240+ ' # BOOTSTRAPPING CODE\n'241 ' environment = context.environment\n'242 ' get_attribute = environment.get_attribute\n'243 ' perform_test = environment.perform_test\n'244 ' apply_filters = environment.apply_filters\n'1 unmodified line246 ' call_function_simple = environment.call_function_simple\n'247 ' finish_var = environment.finish_var'248 ]249 self.indention = 1250+251+ # we have requirements? add them here.252+ if requirements:253+ lines.append(self.indent('# REQUIREMENTS'))254+ for n in requirements:255+ lines.append(self.handle_node(n))256+ lines.append(self.indent('# END OF REQUIREMENTS'))257+258+ # the template body259 rv = self.handle_node_list(node)260 261+ # add translation helpers if required262 if self.require_translations:263 lines.append(264 ' translator = context.get_translator()\n'265 ' def translate(s, p=None, n=None, r=None):\n'193 unmodified lines459 else.460 """461 rv = self.handle_node(node.body)462 if not rv:436- return self.indent('# EMPTY BLOCK (%r:%s)' % (463+ return self.indent('# EMPTY BLOCK FROM %r, LINE %s' % (464 node.filename or '?',465 node.lineno466 ))467 468 buf = []469 write = lambda x: buf.append(self.indent(x))470 444- write('# BLOCK (%r:%s)' % (471+ write('# BLOCK FROM %r, LINE %s' % (472 node.filename or '?',473 node.lineno474 ))475 write('context.push()')317 unmodified linesmodified:jinja/parser.py+1 added-1 removed
155 unmodified lines156 raise ValueError()157 except (StopIteration, ValueError):158 raise TemplateSyntaxError('invalid syntax for set', lineno)159 ast = self.parse_python(lineno, gen, '(%s)')160- return nodes.Set(lineno, name[2], ast.expr)160+ return nodes.Set(lineno, str(name[2]), ast.expr)161 162 def handle_filter_directive(self, lineno, gen):163 """164 Handle {% filter foo|bar %} directives.316 unmodified linesmodified:jinja/utils.py-1 removed
34 unmodified lines35 return Markup(_escape_res[not attribute].sub(lambda m:36 _escape_pairs[m.group()], unicode(x)))37 38 39-39 def find_translations(environment, source):40 """41 Find all translatable strings in a template and yield42 them as (lineno, singular, plural) tuples. If a plural8 unmodified linesmodified:tests/templates/index.html+9 added-1 removed
123456789104111213{% 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 %}