Unified some code in the super/template reference system.
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 1b54f74 · Browse files · 5 files·+45 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+45 added−16 removed
- other:+45 added−16 removed
Provenance
- Commit
c347ed0- Tree
ace1e16- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
1b54f74
5 files changed+45 added−16 removed
modified:docs/templates.rst+3 added-1 removed
344 unmodified lines345 <title>{% block title %}{% endblock %}</title>346 <h1>{{ self.title() }}</h1>347 {% block body %}{% endblock %}348 349-349 Unlike Python Jinja does not support multiple inheritance. So you can only have350 one extends tag called per rendering.351 352 260 unmodified lines613 `caller`614 This is `true` if the macro accesses the special `caller` variable and may615 be called from a :ref:`call<call>` tag.616 617+If a macro name starts with an underscore it's not exported and can't618+be imported.619 620+621 .. _call:622 623 Call624 ~~~~504 unmodified linesmodified:examples/rwbench/rwbench.py+3 added-1 removed
76 unmodified lines77 mako_template.render_unicode(**context)78 79 80 from djangoext import django_loader, DjangoContext81-django_template = django_loader.get_template('index.html')81 def test_django():82+ # not cached because django is not thread safe and does83+ # not cache by itself so it would be unfair to cache it here.84+ django_template = django_loader.get_template('index.html')85 django_template.render(DjangoContext(context))86 87 88 def test_genshi():11 unmodified linesmodified:examples/bench.py+6 added-3 removed
59 unmodified lines60 from django.template import Template as DjangoTemplate, Context as DjangoContext61 except ImportError:62 test_django = None63 else:64- django_template = DjangoTemplate("""\64+ django_template = """\65 <!doctype html>66 <html>67 <head>68 <title>{{ page_title }}</title>19 unmodified lines88 </table>89 </div>90 </body>91 </html>\92-""")92+"""93 94 def test_django():95 c = DjangoContext(context)96 c['navigation'] = [('index.html', 'Index'), ('downloads.html', 'Downloads'),97 ('products.html', 'Products')]98- django_template.render(c)98+ # recompile template each rendering because that's what django99+ # is doing in normal situations too. Django is not thread safe100+ # so we can't cache it in regular apps either.101+ DjangoTemplate(django_template).render(c)102 103 try:104 from mako.template import Template as MakoTemplate105 except ImportError:214 unmodified linesmodified:ext/django2jinja/django2jinja.py+1 added-1 removed
411 unmodified lines412 413 414 @node(loader_tags.BlockNode)415 def block(writer, node):416- writer.tag('block ' + node.name.replace('-', '_'))416+ writer.tag('block ' + node.name.replace('-', '_').rstrip('_'))417 node = node418 while node.parent is not None:419 node = node.parent420 writer.body(node.nodelist)262 unmodified linesmodified:jinja2/runtime.py+32 added-10 removed
77 unmodified lines78 def super(self, name, current):79 """Render a parent block."""80 try:81 blocks = self.blocks[name]82- block = blocks[blocks.index(current) + 1]82+ index = blocks.index(current) + 183+ blocks[index]84 except LookupError:85 return self.environment.undefined('there is no parent block '86 'called %r.' % name,87 name='super')87- wrap = self.environment.autoescape and Markup or (lambda x: x)88- render = lambda: wrap(concat(block(self)))89- render.__name__ = render.name = name90- return render88+ return BlockReference(name, self, blocks, index)89 90 def get(self, key, default=None):91 """Returns an item from the template context, if it doesn't exist92 `default` is returned.86 unmodified lines179 def __init__(self, context):180 self.__context = context181 182 def __getitem__(self, name):185- func = self.__context.blocks[name][0]183+ blocks = self.__context.blocks[name]184 wrap = self.__context.environment.autoescape and \185 Markup or (lambda x: x)188- render = lambda: wrap(concat(func(self.__context)))189- render.__name__ = render.name = name190- return render186+ return BlockReference(name, self.__context, blocks, 0)187 188 def __repr__(self):189 return '<%s %r>' % (190 self.__class__.__name__,195- self._context.name191+ self.__context.name192 )193 194 195+class BlockReference(object):196+ """One block on a template reference."""197+198+ def __init__(self, name, context, stack, depth):199+ self.name = name200+ self._context = context201+ self._stack = stack202+ self._depth = depth203+204+ @property205+ def super(self):206+ """Super the block."""207+ if self._depth + 1 >= len(self._stack):208+ return self._context.environment. \209+ undefined('there is no parent block called %r.' %210+ self.name, name='super')211+ return BlockReference(self.name, self._context, self._stack,212+ self._depth + 1)213+214+ def __call__(self):215+ rv = concat(self._stack[self._depth](self._context))216+ if self._context.environment.autoescape:217+ rv = Markup(rv)218+ return rv219+220+221 class LoopContext(object):222 """A loop context for dynamic iteration."""223 224 def __init__(self, iterable, recurse=None):259 unmodified lines