Unified some code in the super/template reference system.
--HG--
branch : trunk
diff --git a/jinja2/runtime.py b/jinja2/runtime.py
index bb5d9fd..bd7e305 100644
--- a/jinja2/runtime.py
+++ b/jinja2/runtime.py
@@ -79,15 +79,13 @@
"""Render a parent block."""
try:
blocks = self.blocks[name]
- block = blocks[blocks.index(current) + 1]
+ index = blocks.index(current) + 1
+ blocks[index]
except LookupError:
return self.environment.undefined('there is no parent block '
'called %r.' % name,
name='super')
- wrap = self.environment.autoescape and Markup or (lambda x: x)
- render = lambda: wrap(concat(block(self)))
- render.__name__ = render.name = name
- return render
+ return BlockReference(name, self, blocks, index)
def get(self, key, default=None):
"""Returns an item from the template context, if it doesn't exist
@@ -182,20 +180,44 @@
self.__context = context
def __getitem__(self, name):
- func = self.__context.blocks[name][0]
+ blocks = self.__context.blocks[name]
wrap = self.__context.environment.autoescape and \
Markup or (lambda x: x)
- render = lambda: wrap(concat(func(self.__context)))
- render.__name__ = render.name = name
- return render
+ return BlockReference(name, self.__context, blocks, 0)
def __repr__(self):
return '<%s %r>' % (
self.__class__.__name__,
- self._context.name
+ self.__context.name
)
+class BlockReference(object):
+ """One block on a template reference."""
+
+ def __init__(self, name, context, stack, depth):
+ self.name = name
+ self._context = context
+ self._stack = stack
+ self._depth = depth
+
+ @property
+ def super(self):
+ """Super the block."""
+ if self._depth + 1 >= len(self._stack):
+ return self._context.environment. \
+ undefined('there is no parent block called %r.' %
+ self.name, name='super')
+ return BlockReference(self.name, self._context, self._stack,
+ self._depth + 1)
+
+ def __call__(self):
+ rv = concat(self._stack[self._depth](self._context))
+ if self._context.environment.autoescape:
+ rv = Markup(rv)
+ return rv
+
+
class LoopContext(object):
"""A loop context for dynamic iteration."""