Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite docs about extending template objects #1540

Merged
merged 1 commit into from Nov 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 17 additions & 8 deletions docs/templates.rst
Expand Up @@ -587,17 +587,26 @@ When combined with ``scoped``, the ``required`` modifier must be placed
Template Objects
~~~~~~~~~~~~~~~~

.. versionchanged:: 2.4
``extends``, ``include``, and ``import`` can take a template object
instead of the name of a template to load. This could be useful in some
advanced situations, since you can use Python code to load a template
first and pass it in to ``render``.

.. code-block:: python

if debug_mode:
layout = env.get_template("debug_layout.html")
else:
layout = env.get_template("layout.html")

If a template object was passed in the template context, you can
extend from that object as well. Assuming the calling code passes
a layout template as `layout_template` to the environment, this
code works::
user_detail = env.get_template("user/detail.html", layout=layout)

.. code-block:: jinja

{% extends layout_template %}
{% extends layout %}

Previously, the `layout_template` variable had to be a string with
the layout template's filename for this to work.
Note how ``extends`` is passed the variable with the template object
that was passed to ``render``, instead of a string.


HTML Escaping
Expand Down