Skip to content
Myr

basic inheritance works now

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 449167d · Browse files · 4 files+41 added−39 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+41 added−39 removed

  • other:+41 added−39 removed

4 files+41 added−39 removed

modified:jinja2/compiler.py+9 added-6 removed
338 unmodified lines
339 for x in self.blocks), extra=1)340341 def visit_Block(self, node, frame):342 """Call a block and register it for the template."""343 level = 1344 if frame.toplevel:345 # if we know that we are a child template, there is no need to346 # check if we are one347 if self.has_known_extends:348 return349 if self.extends_so_far > 0:348 self.writeline('if parent_root is None:')349 self.indent()350 self.writeline('for event in context.blocks[0][%r](context):' % node.name)350 self.writeline('if parent_root is None:')351 self.indent()352 level += 1353 self.writeline('for event in context.blocks[%r][-1](context):' % node.name)354 self.indent()355 self.writeline('yield event')353 self.outdent(1 + frame.toplevel)356 self.outdent(level)357358 def visit_Extends(self, node, frame):359 """Calls the extender."""360 if not frame.toplevel:
21 unmodified lines
382 if self.has_known_extends:383 raise CompilerExit()384 self.outdent()385383 self.writeline('parent_root = extends(', node, 1)386 self.writeline('parent_root = environment.get_template(', node, 1)387 self.visit(node.template, frame)385 self.write(', context, environment)')388 self.write(', %r).root_render_func' % self.filename)389390 # if this extends statement was in the root level we can take391 # advantage of that information and simplify the generated code392 # in the top level from this point onwards
336 unmodified lines
modified:jinja2/loaders.py+11 added
44 unmodified lines
45 try:46 return f.read().decode(self.encoding)47 finally:48 f.close()495051class DictLoader(BaseLoader):5253 def __init__(self, mapping):54 self.mapping = mapping5556 def get_source(self, environment, template):57 if template in self.mapping:58 return self.mapping[template], template59 raise TemplateNotFound(template)
modified:jinja2/runtime.py+4 added-10 removed
12 unmodified lines
13except ImportError:14 defaultdict = None151617__all__ = ['extends', 'subscribe', 'LoopContext', 'StaticLoopContext',17__all__ = ['subscribe', 'LoopContext', 'StaticLoopContext',18 'TemplateContext', 'Macro', 'Undefined']192021def extends(template_name, context, environment):22 """This loads a template (and evaluates it) and replaces the blocks."""23 template = environment.get_template(template_name, context.filename)24 for name, block in template.blocks.iteritems():25 context.blocks.setdefault(name, []).append(block)26 return template.root_render_func272821def subscribe(obj, argument):22 """Get an item or attribute of an object."""23 try:24 return getattr(obj, str(argument))
24 unmodified lines
49 dict.__init__(self, globals)50 self.exported = set()51 self.filename = filename52 self.blocks = dict((k, [v]) for k, v in blocks.iteritems())61 self.stream_muted = False53 if isinstance(globals, TemplateContext):54 for name, parent_blocks in globals.blocks.iteritems():55 self.blocks.setdefault(name, []).extend(parent_blocks)5657 def __setitem__(self, key, value):58 """If we set items to the dict we track the variables set so59 that includes can access the exported variables."""
159 unmodified lines
modified:test.py+17 added-23 removed
1from jinja2 import Environment2from jinja2.loaders import DictLoader33env = Environment()4tmpl = env.from_string("""<!doctype html>5<html>6 <head>7 <title>{{ page_title|e }}</title>8 </head>9 <body>10 <ul class="navigation">11 {%- for href, caption in [12 ('index.html', 'Index'),13 ('projects.html', 'Projects'),14 ('about.html', 'About')15 ] %}16 <li><a href="{{ href|e }}">{{ caption|e }}</a></li>17 {%- endfor %}18 </ul>19 <div class="body">20 {{ body }}21 </div>22 </body>23</html>\24""")4env = Environment(loader=DictLoader({5'child.html': u'''\6{% extends master_layout or 'master.html' %}7{% macro get_the_answer() %}42{% endmacro %}8{% block body %}9 {{ get_the_answer() }}10{% endblock %}11''',12'master.html': u'''\13<!doctype html>14<title>Foo</title>15{% block body %}{% endblock %}16'''17}))181927print tmpl.render(page_title='<foo>', body='<p>Hello World</p>')20tmpl = env.get_template("child.html")21print tmpl.render()