summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorAmy <leiamy12@gmail.com>2020-06-10 16:09:09 -0400
committerAmy <leiamy12@gmail.com>2021-01-29 10:28:39 -0500
commit8da77f9753be0c4357daf2ac8b53730a2d977fdf (patch)
treed2fe10c5e355c76a7972b1dc89acad4b3afdc820 /docs
parentbc22a8b0d3453f880d7a8ae8b84e385d1c918b02 (diff)
downloadjinja2-8da77f9753be0c4357daf2ac8b53730a2d977fdf.tar.gz
add required attribute to blocks
required blocks must be overridden at some point, although not necessarily by the direct child template
Diffstat (limited to 'docs')
-rw-r--r--docs/templates.rst34
1 files changed, 34 insertions, 0 deletions
diff --git a/docs/templates.rst b/docs/templates.rst
index 14de875..58ed870 100644
--- a/docs/templates.rst
+++ b/docs/templates.rst
@@ -540,6 +540,40 @@ modifier to a block declaration::
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.
+
+For example::
+
+ # parent.tmpl
+ body: {% block body required %}{% endblock %}
+
+ # child.tmpl
+ {% extends "parent.tmpl" %}
+
+ # grandchild.tmpl
+ {% extends "child.tmpl" %}
+ {% block body %}Hi from grandchild.{% endblock %}
+
+
+Rendering ``child.tmpl`` will give
+``TemplateRuntimeError``
+
+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::
+
+ {% block body scoped %}{% endblock %}
+ {% block body required %}{% endblock %}
+ {% block body scoped required %}{% endblock %}
+
+
Template Objects
~~~~~~~~~~~~~~~~