Skip to content
Myr

added super()

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 fed44b5 · Browse files · 3 files+44 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+44 added−3 removed

  • other:+44 added−3 removed

3 files+44 added−3 removed

added:examples/inheritance.py+12 added
1from jinja2 import Environment2from jinja2.loaders import DictLoader345env = Environment(loader=DictLoader({6'a': '''[A[{% block body %}{% endblock %}]]''',7'b': '''{% extends 'a' %}{% block body %}[B]{% endblock %}''',8'c': '''{% extends 'b' %}{% block body %}###{{ super() }}###{% endblock %}'''9}))101112print env.get_template('c').render()
modified:jinja2/compiler.py+2 added
489 unmodified lines
490 for name, block in self.blocks.iteritems():491 block_frame = Frame()492 block_frame.inspect(block.body)493 block_frame.block = name494 block_frame.identifiers.add_special('super')495 block_frame.name_overrides['super'] = 'context.super(%r)' % name496 self.writeline('def block_%s(context, environment=environment):'497 % name, block, 1)498 self.pull_locals(block_frame)499 self.blockvisit(block.body, block_frame)
531 unmodified lines
modified:jinja2/runtime.py+30 added-3 removed
54 unmodified lines
55 # this is used for includes for example but otherwise, if the globals56 # are a template context, this template is participating in a template57 # inheritance chain and we have to copy the blocks over.58 if not standalone and isinstance(globals, TemplateContext):59 for name, parent_blocks in globals.blocks.iteritems():60 self.blocks.setdefault(name, []).extend(parent_blocks)59 for name, parent_blocks in globals.blocks.iteritems():60 self.blocks.setdefault(name, []).extend(parent_blocks)6162 def super(self, block):63 """Render a parent block."""64 try:65 func = self.blocks[block][-2]66 except LookupError:67 return Undefined('super', extra='there is probably no parent '68 'block with this name')69 return SuperBlock(block, self, func)7071 def __setitem__(self, key, value):72 """If we set items to the dict we track the variables set so73 that includes can access the exported variables."""74 dict.__setitem__(self, key, value)
25 unmodified lines
100 self.filename101 )102103104class SuperBlock(object):105 """When called this renders a parent block."""106107 def __init__(self, name, context, render_func):108 self.name = name109 self._context = context110 self._render_func = render_func111112 def __call__(self):113 return TemplateData(u''.join(self._render_func(self._context)))114115 def __repr__(self):116 return '<%s %r>' % (117 self.__class__.__name__,118 self.name119 )120121122class IncludedTemplate(object):123 """Represents an included template."""124125 def __init__(self, environment, context, template):
156 unmodified lines
282 if extra is not None:283 self._undefined_hint += ' (' + extra + ')'284285 def fail(self, *args, **kwargs):259 raise TypeError(self._undefined_hint)286 raise NameError(self._undefined_hint)287 __getattr__ = __getitem__ = __add__ = __mul__ = __div__ = \288 __realdiv__ = __floordiv__ = __mod__ = __pos__ = __neg__ = \289 __call__ = fail290 del fail
13 unmodified lines