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
Provenance
- Commit
62f8a29- Tree
00d464f- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
fed44b5
3 files changed+44 added−3 removed
added:examples/inheritance.py+12 added
1+from jinja2 import Environment2+from jinja2.loaders import DictLoader3+4+5+env = 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+}))10+11+12+print env.get_template('c').render()modified:jinja2/compiler.py+2 added
489 unmodified lines490 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 linesmodified:jinja2/runtime.py+30 added-3 removed
54 unmodified lines55 # 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)61 62+ 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)70+71 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 lines100 self.filename101 )102 103 104+class SuperBlock(object):105+ """When called this renders a parent block."""106+107+ def __init__(self, name, context, render_func):108+ self.name = name109+ self._context = context110+ self._render_func = render_func111+112+ def __call__(self):113+ return TemplateData(u''.join(self._render_func(self._context)))114+115+ def __repr__(self):116+ return '<%s %r>' % (117+ self.__class__.__name__,118+ self.name119+ )120+121+122 class IncludedTemplate(object):123 """Represents an included template."""124 125 def __init__(self, environment, context, template):156 unmodified lines282 if extra is not None:283 self._undefined_hint += ' (' + extra + ')'284 285 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 fail13 unmodified lines