Skip to content
Myr

Write a new documentation section on nesting extends.

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

authored by Scott Crosby · committed by jab · parent a486b37 · Browse files · 1 file+30 added−0 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+30 added−0 removed

  • other:+30 added−0 removed

1 file+30 added−0 removed

modified:docs/templates.rst+30 added
433 unmodified lines
434 {{ super() }}435 {% endblock %}436437438Nesting template extends.439~~~~~~~~~~~~~~~~~~~~~~~~~440441In the case of multiple levels of %extends, the childmost {% block %} substitutes into the parent template. For instance,442443::444445 #a1.tmpl446 From A1: {% block body %} Want-this-to-be-included. {% endblock %}447448 #a2.tmpl449 {% extends "a1.tmpl" %}450 {% block body %} Want-this-to-be-skipped. {{super()}} {% endblock %}451452 #a3.tmpl453 {% extends "a2.tmpl" %}454 {% block body %} Some text {% endblock %}455456 #a4.tmpl457 {% extends "a2.tmpl" %}458 {% block body %} From A4: {{super.super()}} {% endblock %}459460461Rendering `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.462463Rendering `a3.tmpl` will output `From A1: Some Text`. We substitute the block text into the parent template.464465Rendering `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.`.466467468Named Block End-Tags469~~~~~~~~~~~~~~~~~~~~470471Jinja2 allows you to put the name of the block after the end tag for better
1260 unmodified lines