Skip to content
Myr

Merge pull request #1540 from pallets/docs-macro-defaults

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

rewrite docs about extending template objects

authored by David Lord · committed by GitHub · parents cab5545, a6162da · Browse files · 1 file+17 added−8 removed
This commit

State

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

Diff stats

1 file+17 added−8 removed

  • other:+17 added−8 removed

1 file+17 added−8 removed

modified:docs/templates.rst+17 added-8 removed
585 unmodified lines
586587Template Objects588~~~~~~~~~~~~~~~~589590.. versionchanged:: 2.4590``extends``, ``include``, and ``import`` can take a template object591instead of the name of a template to load. This could be useful in some592advanced situations, since you can use Python code to load a template593first and pass it in to ``render``.594592If a template object was passed in the template context, you can593extend from that object as well. Assuming the calling code passes594a layout template as `layout_template` to the environment, this595code works::595.. code-block:: python596597 {% extends layout_template %}597 if debug_mode:598 layout = env.get_template("debug_layout.html")599 else:600 layout = env.get_template("layout.html")601599Previously, the `layout_template` variable had to be a string with600the layout template's filename for this to work.602 user_detail = env.get_template("user/detail.html", layout=layout)603604.. code-block:: jinja605606 {% extends layout %}607608Note how ``extends`` is passed the variable with the template object609that was passed to ``render``, instead of a string.610611612HTML Escaping613-------------614615When generating HTML from templates, there's always a risk that a variable will
1279 unmodified lines