<h1>{{ self.title() }}</h1>
{% block body %}{% endblock %}
-
Unlike Python Jinja does not support multiple inheritance. So you can only have
one extends tag called per rendering.
This is `true` if the macro accesses the special `caller` variable and may
be called from a :ref:`call<call>` tag.
+If a macro name starts with an underscore it's not exported and can't
+be imported.
+
.. _call:
except ImportError:
test_django = None
else:
- django_template = DjangoTemplate("""\
+ django_template = """\
<!doctype html>
<html>
<head>
</div>
</body>
</html>\
-""")
+"""
def test_django():
c = DjangoContext(context)
c['navigation'] = [('index.html', 'Index'), ('downloads.html', 'Downloads'),
('products.html', 'Products')]
- django_template.render(c)
+ # recompile template each rendering because that's what django
+ # is doing in normal situations too. Django is not thread safe
+ # so we can't cache it in regular apps either.
+ DjangoTemplate(django_template).render(c)
try:
from mako.template import Template as MakoTemplate
"""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
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."""