[svn] added a sanity check for block tags outside the root level
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 c7ddd1d · Browse files · 4 files·+105 added−33 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+105 added−33 removed
- other:+105 added−33 removed
Provenance
- Commit
4f41711- Tree
0469e85- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
c7ddd1d
4 files changed+105 added−33 removed
modified:docs/src/designerdoc.txt+44 added
609 unmodified lines610 also defines the content that fills the hole in the *parent*. If there were611 two similarly-named ``{% block %}`` tags in a template, that template's612 parent wouldn't know which one of the blocks' content to use.613 614+How Inheritance Works Internally615+--------------------------------616 617+Inheritance in Jinja is straighforward. If a template contains a618+``{% extends %}`` tag it's considered being a child template, otherwise it's619+a layout template. In a layout template you can place blocks basically620+everywhere. In a child template blocks can only be located either top level621+or inside of another block.622+623+Data outside of a block in a child template is executed before the layout624+template is rendered, thus you can use it to propagate data to the whole625+inheritance chain. Having a block in an invalid position you will receive626+an syntax error. Here some examples:627+628+**impossible**:629+630+ .. sourcecode:: jinja631+632+ {% extends 'master.html' %}633+ {% if some_condition %}634+ {% block body %}635+ ...636+ {% endblock %}637+ {% endif %}638+639+ This can't work because template inheritance works at translation /640+ compilation time not at template executation.641+642+**possible**:643+644+ .. sourcecode:: jinja645+646+ {% extends 'master.html' %}647+ {% block body %}648+ {% if some_condition %}649+ {% block myblock %}650+ ...651+ {% endblock %}652+ {% endblock %}653+ {% endblock %}654+655+ This can work although it probably makes no sense in this specific case.656+ However the condition is handled at runtime because it's in a valid block657+ and defines a new block subtemplates can override.658+659 Super Blocks660 ============661 662 Starting with `Jinja 1.1` it's possible to render the contents of the parent474 unmodified linesmodified:docs/src/i18n.txt+4 added
48 unmodified lines49 env = ApplicationEnvironment()50 tmpl = env.get_template('index.html')51 tmpl.render(LANGUAGE='de_DE')52 53+This example assumes that you use gettext and have a gettext54+`Translations` object which is returned by the `get_translator`55+function.56 57+58 Collecting Translations59 =======================60 61 The next step is to collect the translations. Every Jinja environment20 unmodified linesmodified:jinja/parser.py+55 added-33 removed
572 unmodified lines573 names escaped by the lexer (unused python keywords) and set the574 filename attributes for all nodes in the tree.575 """576 body = self.subparse(None)577- todo = [body]578- while todo:579- node = todo.pop()580- # all names excluding keywords have an trailing underline.581- # if we find a name without trailing underline that's a keyword582- # and this code raises an error. else strip the underline again583- if node.__class__ in (ast.AssName, ast.Name, ast.Keyword):584- if not node.name.endswith('_'):585- raise TemplateSyntaxError('illegal use of keyword %r '586- 'as identifier.' % node.name,587- node.lineno, self.filename)588- node.name = node.name[:-1]589- # same for attributes590- elif node.__class__ is ast.Getattr:591- if not node.attrname.endswith('_'):592- raise TemplateSyntaxError('illegal use of keyword %r '593- 'as attribute name.' %594- node.name, node.lineno,595- self.filename)596- node.attrname = node.attrname[:-1]597- # if we have a ForLoop we ensure that nobody patches existing598- # object using "for foo.bar in seq"599- elif node.__class__ is nodes.ForLoop:600- def check(node):601- if node.__class__ not in (ast.AssName, ast.AssTuple):602- raise TemplateSyntaxError('can\'t assign to '603- 'expression.', node.lineno,577+ def walk(nodes_, stack):578+ for node in nodes_:579+ # all names excluding keywords have an trailing underline.580+ # if we find a name without trailing underline that's a581+ # keyword and this code raises an error. else strip the582+ # underline again583+ if node.__class__ in (ast.AssName, ast.Name, ast.Keyword):584+ if not node.name.endswith('_'):585+ raise TemplateSyntaxError('illegal use of keyword %r '586+ 'as identifier.' %587+ node.name, node.lineno,588 self.filename)589+ node.name = node.name[:-1]590+ # same for attributes591+ elif node.__class__ is ast.Getattr:592+ if not node.attrname.endswith('_'):593+ raise TemplateSyntaxError('illegal use of keyword %r '594+ 'as attribute name.' %595+ node.name, node.lineno,596+ self.filename)597+ node.attrname = node.attrname[:-1]598+ # if we have a ForLoop we ensure that nobody patches existing599+ # object using "for foo.bar in seq"600+ elif node.__class__ is nodes.ForLoop:601+ def check(node):602+ if node.__class__ not in (ast.AssName, ast.AssTuple):603+ raise TemplateSyntaxError('can\'t assign to '604+ 'expression.',605+ node.lineno,606+ self.filename)605- for n in node.getChildNodes():606- check(n)607- check(node.item)608- # now set the filename and continue working on the childnodes609- node.filename = self.filename610- todo.extend(node.getChildNodes())607+ for n in node.getChildNodes():608+ check(n)609+ check(node.item)610+ # ensure that in child templates block are either top level611+ # or located inside of another block tag.612+ elif self.extends is not None and \613+ node.__class__ is nodes.Block:614+ if stack[-1] is not body:615+ for n in stack:616+ if n.__class__ is nodes.Block:617+ break618+ else:619+ raise TemplateSyntaxError('misplaced block %r, '620+ 'blocks in child '621+ 'templates must be '622+ 'either top level or '623+ 'located in a block '624+ 'tag.' % node.name,625+ node.lineno,626+ self.filename)627+ # now set the filename and continue working on the childnodes628+ node.filename = self.filename629+ stack.append(node)630+ walk(node.getChildNodes(), stack)631+ stack.pop()632+ walk([body], [body])633 return nodes.Template(self.filename, body, self.extends)634 635 def subparse(self, test, drop_needle=False):636 """152 unmodified linesmodified:CHANGES+2 added
79 unmodified lines80 - implemented `{% call %}` - unsure if this makes it into the final release.81 82 - it's not possible to stream templates.83 84+- fixed a corner case when defining a block inside of a condition85 86+87 Version 1.088 -----------89 (released Mar 23, 2007)90 1 unmodified line