summaryrefslogtreecommitdiff
path: root/docs/ref
diff options
context:
space:
mode:
Diffstat (limited to 'docs/ref')
-rw-r--r--docs/ref/templates/builtins.txt74
-rw-r--r--docs/ref/templates/language.txt144
2 files changed, 218 insertions, 0 deletions
diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt
index 1eb391d8c0..464c4d83ac 100644
--- a/docs/ref/templates/builtins.txt
+++ b/docs/ref/templates/builtins.txt
@@ -957,6 +957,80 @@ output (as a string) inside a variable. This is useful if you want to use
{% now "Y" as current_year %}
{% blocktranslate %}Copyright {{ current_year }}{% endblocktranslate %}
+.. templatetag:: partial
+
+``partial``
+-----------
+
+.. versionadded:: 6.0
+
+Renders a template fragment that was defined with :ttag:`partialdef`, inserting
+the matching partial at this location.
+
+Usage:
+
+.. code-block:: html+django
+
+ {% partial partial_name %}
+
+The ``partial_name`` argument is the name of the template fragment to render.
+
+In the following example, a partial named ``button`` is defined and then
+rendered three times:
+
+.. code-block:: html+django
+
+ {% partialdef button %}
+ <button>Submit</button>
+ {% endpartialdef %}
+
+ {% partial button %}
+ {% partial button %}
+ {% partial button %}
+
+.. templatetag:: partialdef
+
+``partialdef``
+--------------
+
+.. versionadded:: 6.0
+
+Defines a reusable template fragment that can be rendered multiple times within
+the same template or accessed directly via :ref:`template loading or inclusion
+<template-partials-direct-access>`.
+
+Usage:
+
+.. code-block:: html+django
+
+ {% partialdef partial_name %}
+ {# Reusable content. #}
+ {% endpartialdef %}
+
+The ``partial_name`` argument is required and must be a valid template
+identifier.
+
+In the following example, a new fragment named ``card`` is defined:
+
+.. code-block:: html+django
+
+ {% partialdef card %}
+ <div class="card">
+ <h3>{{ title }}</h3>
+ <p>{{ content }}</p>
+ </div>
+ {% endpartialdef %}
+
+This partial can then be rendered using the :ttag:`partial` tag:
+
+.. code-block:: html+django
+
+ {% partial card %}
+ {% partial card %}
+
+To :ref:`render a fragment immediately in place <template-partials-inline>`,
+use the ``inline`` option. The partial is still stored and can be reused later.
+
.. templatetag:: querystring
``querystring``
diff --git a/docs/ref/templates/language.txt b/docs/ref/templates/language.txt
index 16a3682086..e8812666fa 100644
--- a/docs/ref/templates/language.txt
+++ b/docs/ref/templates/language.txt
@@ -475,6 +475,150 @@ it also defines the content that fills the hole in the *parent*. If there were
two similarly-named :ttag:`block` tags in a template, that template's parent
wouldn't know which one of the blocks' content to use.
+.. _template-partials:
+
+Template partials
+=================
+
+.. versionadded:: 6.0
+
+Template partials define reusable template fragments within a template file.
+They are self-contained components that can be rendered multiple times within
+the same template, helping to avoid repetition and maintain consistent output.
+
+Basic syntax
+------------
+
+A partial can be defined using the :ttag:`partialdef` tag:
+
+.. code-block:: html+django
+ :caption: ``authors.html``
+
+ {% partialdef user-info %}
+ <div id="user-info-{{ user.username }}">
+ <h3>{{ user.name }}</h3>
+ <p>{{ user.bio }}</p>
+ </div>
+ {% endpartialdef %}
+
+For extra readability, the name can be included in the ``{% endpartialdef %}``
+closing tag:
+
+.. code-block:: html+django
+
+ {% partialdef user-info %}
+ {# ... #}
+ {% endpartialdef user-info %}
+
+The template fragment can be rendered using the :ttag:`partial` tag:
+
+.. code-block:: html+django
+
+ {% partial user-info %}
+
+Fragment reuse
+--------------
+
+A template fragment can be reused multiple times:
+
+.. code-block:: html+django
+ :caption: ``authors.html``
+
+ {% block content %}
+ <h2>Authors</h2>
+ {% for user in authors %}
+ {% partial user-info %}
+ {% endfor %}
+
+ <h2>Editors</h2>
+ {% for user in editors %}
+ {% partial user-info %}
+ {% endfor %}
+ {% endblock %}
+
+The partial content is rendered each time the named partial is used, with the
+current template context.
+
+.. _template-partials-inline:
+
+Inline partials
+---------------
+
+A template fragment can be defined and rendered in-place using the ``inline``
+argument. This defines the partial for later reuse while also rendering it
+immediately at its definition:
+
+.. code-block:: html+django
+
+ {# Define and render immediately. #}
+ {% partialdef user-info inline %}
+ <div id="user-info-{{ user.username }}">
+ <h3>{{ user.name }}</h3>
+ <p>{{ user.bio }}</p>
+ </div>
+ {% endpartialdef %}
+
+ {# Other page content here. #}
+
+ {# Reuse later elsewhere in the template. #}
+ <section class="featured-authors">
+ <h2>Featured Authors</h2>
+ {% for user in featured %}
+ {% partial user-info %}
+ {% endfor %}
+ </section>
+
+.. _template-partials-direct-access:
+
+Accessing partials directly
+---------------------------
+
+Template fragments defined with :ttag:`partialdef` can be accessed directly via
+template loading or inclusion using the ``template.html#partial_name`` syntax.
+
+For example, using the :func:`~django.shortcuts.render` shortcut, the following
+code renders only the partial named ``user-info`` defined in the
+``authors.html`` template::
+
+ from django.contrib.auth.models import User
+ from django.shortcuts import get_object_or_404, render
+
+
+ def user_info_partial(request, user_id):
+ user = get_object_or_404(User, id=user_id)
+ return render(request, "authors.html#user-info", {"user": user})
+
+This approach is particularly useful for AJAX-style requests that update only
+specific portions of a page with the rendered template fragment.
+
+Template partials can also be included using the :ttag:`include` template tag
+with the same ``#`` directive:
+
+.. code-block:: html+django
+
+ {% include "authors.html#user-info" %}
+
+Context handling
+----------------
+
+Template partials are rendered with the current template context. They work as
+expected in loops and with context variables:
+
+.. code-block:: html+django
+
+ {% for user in users %}
+ {% partial user-info %}
+ {% endfor %}
+
+Context variables can be adjusted using the :ttag:`with` tag:
+
+.. code-block:: html+django
+
+ {% with user=featured_author %}
+ <h2>Featured Author of the Month</h2>
+ {% partial user-info %}
+ {% endwith %}
+
.. _next section: #automatic-html-escaping
.. _automatic-html-escaping: