From: David Lord Date: Sat, 30 Jan 2021 13:47:23 +0000 (-0800) Subject: update docs about required blocks (#1340) X-Git-Tag: 3.0.0rc1~48 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c3b34a06f340234939df5ad77bbe6327ca7fc3f0;p=thirdparty%2Fjinja.git update docs about required blocks (#1340) --- diff --git a/CHANGES.rst b/CHANGES.rst index 658d2d37..b5c4df29 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -19,9 +19,10 @@ Unreleased - Fix UndefinedError incorrectly being thrown on an undefined variable instead of ``Undefined`` being returned on ``NativeEnvironment`` on Python 3.10. :issue:`1335` -- Add ``required`` attribute to blocks that must be overridden at some - point, but not necessarily by the direct child :issue:`1147` -- Deprecate ``autoescape`` and ``with`` extensions :issue:`1203` +- Blocks can be marked as ``required``. They must be overridden at + some point, but not necessarily by the direct child. :issue:`1147` +- Deprecate the ``autoescape`` and ``with`` extensions, they are + built-in to the compiler. :issue:`1203` Version 2.11.2 diff --git a/docs/templates.rst b/docs/templates.rst index 58ed870e..6226468d 100644 --- a/docs/templates.rst +++ b/docs/templates.rst @@ -541,33 +541,38 @@ When overriding a block, the `scoped` modifier does not have to be provided. Required Blocks -~~~~~~~~~~~~~~~~~~~~~~~ +~~~~~~~~~~~~~~~ -Blocks can be marked as required. They must be overridden at some point, -but not necessarily by the direct child template. Required blocks can -only contain whitespace or comments, and they cannot be rendered directly. +Blocks can be marked as ``required``. They must be overridden at some +point, but not necessarily by the direct child template. Required blocks +may only contain space and comments, and they cannot be rendered +directly. -For example:: +.. code-block:: jinja + :caption: ``page.txt`` - # parent.tmpl - body: {% block body required %}{% endblock %} + {% block body required %}{% endblock %} - # child.tmpl - {% extends "parent.tmpl" %} +.. code-block:: jinja + :caption: ``issue.txt`` - # grandchild.tmpl - {% extends "child.tmpl" %} - {% block body %}Hi from grandchild.{% endblock %} + {% extends "page.txt" %} +.. code-block:: jinja + :caption: ``bug_report.txt`` -Rendering ``child.tmpl`` will give -``TemplateRuntimeError`` + {% extends "issue.txt" %} + {% block body %}Provide steps to demonstrate the bug.{% endblock %} + +Rendering ``page.txt`` or ``issue.txt`` will raise +``TemplateRuntimeError`` because they don't override the ``body`` block. +Rendering ``bug_report.txt`` will succeed because it does override the +block. -Rendering ``grandchild.tmpl`` will give -``Hi from grandchild.`` +When combined with ``scoped``, the ``required`` modifier must be placed +*after* the scoped modifier. Here are some valid examples: -When combined with ``scoped``, the ``required`` modifier must be placed `after` -the scoped modifier. Here are some valid examples:: +.. code-block:: jinja {% block body scoped %}{% endblock %} {% block body required %}{% endblock %}