Skip to content
Myr

[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

4 files+105 added−33 removed

modified:docs/src/designerdoc.txt+44 added
609 unmodified lines
610 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.613614How Inheritance Works Internally615--------------------------------616617Inheritance in Jinja is straighforward. If a template contains a618``{% extends %}`` tag it's considered being a child template, otherwise it's619a layout template. In a layout template you can place blocks basically620everywhere. In a child template blocks can only be located either top level621or inside of another block.622623Data outside of a block in a child template is executed before the layout624template is rendered, thus you can use it to propagate data to the whole625inheritance chain. Having a block in an invalid position you will receive626an syntax error. Here some examples:627628**impossible**:629630 .. sourcecode:: jinja631632 {% extends 'master.html' %}633 {% if some_condition %}634 {% block body %}635 ...636 {% endblock %}637 {% endif %}638639 This can't work because template inheritance works at translation /640 compilation time not at template executation.641642**possible**:643644 .. sourcecode:: jinja645646 {% extends 'master.html' %}647 {% block body %}648 {% if some_condition %}649 {% block myblock %}650 ...651 {% endblock %}652 {% endblock %}653 {% endblock %}654655 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.658659Super Blocks660============661662Starting with `Jinja 1.1` it's possible to render the contents of the parent
474 unmodified lines
modified:docs/src/i18n.txt+4 added
48 unmodified lines
49 env = ApplicationEnvironment()50 tmpl = env.get_template('index.html')51 tmpl.render(LANGUAGE='de_DE')5253This example assumes that you use gettext and have a gettext54`Translations` object which is returned by the `get_translator`55function.565758Collecting Translations59=======================6061The next step is to collect the translations. Every Jinja environment
20 unmodified lines
modified:jinja/parser.py+55 added-33 removed
572 unmodified lines
573 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)634635 def subparse(self, test, drop_needle=False):636 """
152 unmodified lines
modified:CHANGES+2 added
79 unmodified lines
80- implemented `{% call %}` - unsure if this makes it into the final release.8182- it's not possible to stream templates.8384- fixed a corner case when defining a block inside of a condition858687Version 1.088-----------89(released Mar 23, 2007)90
1 unmodified line