Skip to content
Myr

Improve nested extends (super.super) example.

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

authored by jab · committed by jab · parent b5c6c9c · Browse files · 1 file+24 added−19 removed
This commit

State

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

Diff stats

1 file+24 added−19 removed

  • other:+24 added−19 removed

1 file+24 added−19 removed

modified:docs/templates.rst+24 added-19 removed
423 unmodified lines
424425Super Blocks426~~~~~~~~~~~~427428It's possible to render the contents of the parent block by calling `super`.428It's possible to render the contents of the parent block by calling ``super()``.429This gives back the results of the parent block::430431 {% block sidebar %}432 <h3>Table Of Contents</h3>
1 unmodified line
434 {{ super() }}435 {% endblock %}436437438Nesting template extends.439~~~~~~~~~~~~~~~~~~~~~~~~~438Nesting extends439~~~~~~~~~~~~~~~440441In the case of multiple levels of %extends, the childmost {% block %} substitutes into the parent template. For instance,441In the case of multiple levels of ``{% extends %}``,442``super`` references may be chained (as in ``super.super()``)443to skip levels in the inheritance tree.444443::445For example::446445 #a1.tmpl446 From A1: {% block body %} Want-this-to-be-included. {% endblock %}447 # parent.tmpl448 body: {% block body %}Hi from parent.{% endblock %}449448 #a2.tmpl449 {% extends "a1.tmpl" %}450 {% block body %} Want-this-to-be-skipped. {{super()}} {% endblock %}450 # child.tmpl451 {% extends "parent.tmpl" %}452 {% block body %}Hi from child. {{ super() }}{% endblock %}453452 #a3.tmpl453 {% extends "a2.tmpl" %}454 {% block body %} Some text {% endblock %}454 # grandchild1.tmpl455 {% extends "child.tmpl" %}456 {% block body %}Hi from grandchild1.{% endblock %}457456 #a4.tmpl457 {% extends "a2.tmpl" %}458 {% block body %} From A4: {{super.super()}} {% endblock %}458 # grandchild2.tmpl459 {% extends "child.tmpl" %}460 {% block body %}Hi from grandchild2. {{ super.super() }} {% endblock %}461462461Rendering `a2.tmpl` will output `From A1: Want-this-to-be-skipped`. The text `Want-this-to-be-included.` is rendered because the `super()` call render as the text from the parent template into the block, which will then be substitute into topmost template.463Rendering ``child.tmpl`` will give464``body: Hi from child. Hi from parent.``465463Rendering `a3.tmpl` will output `From A1: Some Text`. We substitute the block text into the parent template.466Rendering ``grandchild1.tmpl`` will give467``body: Hi from grandchild1.``468465Rendering `a4.tmpl` will output `From A1: From A4: Want-this-to-be-included.` Here, we are accessing the `super` property from the `super` object to get access to the text in grandparent's block. Had we just done `super()`, this would have returned `From A1: From A4: Want-this-to-be-skipped. Want-this-to-be-included.`.469Rendering ``grandchild2.tmpl`` will give470``body: Hi from grandchild2. Hi from parent.``471472473Named Block End-Tags474~~~~~~~~~~~~~~~~~~~~
1262 unmodified lines