Skip to content
Myr

update docs about required blocks (#1340)

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

authored by David Lord · committed by GitHub · parent b5a00c8 · Browse files · 2 files+27 added−21 removed
This commit

State

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

Diff stats

2 files+27 added−21 removed

  • other:+27 added−21 removed

2 files+27 added−21 removed

modified:docs/templates.rst+23 added-18 removed
539 unmodified lines
540When overriding a block, the `scoped` modifier does not have to be provided.541542543Required Blocks544~~~~~~~~~~~~~~~~~~~~~~~544~~~~~~~~~~~~~~~545546Blocks can be marked as required. They must be overridden at some point,547but not necessarily by the direct child template. Required blocks can548only contain whitespace or comments, and they cannot be rendered directly.546Blocks can be marked as ``required``. They must be overridden at some547point, but not necessarily by the direct child template. Required blocks548may only contain space and comments, and they cannot be rendered549directly.550550For example::551.. code-block:: jinja552 :caption: ``page.txt``553552 # parent.tmpl553 body: {% block body required %}{% endblock %}554 {% block body required %}{% endblock %}555555 # child.tmpl556 {% extends "parent.tmpl" %}556.. code-block:: jinja557 :caption: ``issue.txt``558558 # grandchild.tmpl559 {% extends "child.tmpl" %}559 {% extends "page.txt" %}560 {% block body %}Hi from grandchild.{% endblock %}560561.. code-block:: jinja562 :caption: ``bug_report.txt``563563Rendering ``child.tmpl`` will give564``TemplateRuntimeError``564 {% extends "issue.txt" %}565 {% block body %}Provide steps to demonstrate the bug.{% endblock %}566566Rendering ``grandchild.tmpl`` will give567``Hi from grandchild.``567Rendering ``page.txt`` or ``issue.txt`` will raise568``TemplateRuntimeError`` because they don't override the ``body`` block.569Rendering ``bug_report.txt`` will succeed because it does override the570block.571569When combined with ``scoped``, the ``required`` modifier must be placed `after`570the scoped modifier. Here are some valid examples::572When combined with ``scoped``, the ``required`` modifier must be placed573*after* the scoped modifier. Here are some valid examples:574575.. code-block:: jinja576577 {% block body scoped %}{% endblock %}578 {% block body required %}{% endblock %}579 {% block body scoped required %}{% endblock %}580
1287 unmodified lines
modified:CHANGES.rst+4 added-3 removed
17 unmodified lines
18 constant input. :issue:`1279`19- Fix UndefinedError incorrectly being thrown on an undefined variable20 instead of ``Undefined`` being returned on21 ``NativeEnvironment`` on Python 3.10. :issue:`1335`22- Add ``required`` attribute to blocks that must be overridden at some23 point, but not necessarily by the direct child :issue:`1147`24- Deprecate ``autoescape`` and ``with`` extensions :issue:`1203`22- Blocks can be marked as ``required``. They must be overridden at23 some point, but not necessarily by the direct child. :issue:`1147`24- Deprecate the ``autoescape`` and ``with`` extensions, they are25 built-in to the compiler. :issue:`1203`262728Version 2.11.229--------------
770 unmodified lines