Skip to content

Commit 99daa2a

Browse files
authored
Merge pull request #1540 from pallets/docs-macro-defaults
rewrite docs about extending template objects
2 parents cab5545 + a6162da commit 99daa2a

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

docs/templates.rst

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -587,17 +587,26 @@ When combined with ``scoped``, the ``required`` modifier must be placed
587587
Template Objects
588588
~~~~~~~~~~~~~~~~
589589

590-
.. versionchanged:: 2.4
590+
``extends``, ``include``, and ``import`` can take a template object
591+
instead of the name of a template to load. This could be useful in some
592+
advanced situations, since you can use Python code to load a template
593+
first and pass it in to ``render``.
594+
595+
.. code-block:: python
596+
597+
if debug_mode:
598+
layout = env.get_template("debug_layout.html")
599+
else:
600+
layout = env.get_template("layout.html")
591601
592-
If a template object was passed in the template context, you can
593-
extend from that object as well. Assuming the calling code passes
594-
a layout template as `layout_template` to the environment, this
595-
code works::
602+
user_detail = env.get_template("user/detail.html", layout=layout)
603+
604+
.. code-block:: jinja
596605
597-
{% extends layout_template %}
606+
{% extends layout %}
598607
599-
Previously, the `layout_template` variable had to be a string with
600-
the layout template's filename for this to work.
608+
Note how ``extends`` is passed the variable with the template object
609+
that was passed to ``render``, instead of a string.
601610

602611

603612
HTML Escaping

0 commit comments

Comments
 (0)