With the partial defined, you can reuse it multiple times later:
{% block main %}
BEGINNING
{% partial test-partial %}
MIDDLE
{% partial test-partial %}
END
{% endblock main %}
The partial content will be rendered in each time the named partial is used.
Via the template loader
django-template-partials is also integrated with the template loader, so you
can pass a template plus a partial name to the loader to have just that part
rendered:
# In view handler…
self.template_name = "example.html#test-partial"
The rest of your view logic remains the same.
This means that you can also use the partial with the include tag:
{% include "example.html#test-partial" %}
Outputting inline
You might want to wrap an existing part of your page, and continue rendering
the content inside your partial, use the inline argument in that situation:
{% block main %}
{% partialdef inline-partial inline %}
CONTENT
{% endpartialdef %}
{% endblock main %}
Controlling the context
A template partial is rendered with the current context.
This means it works in, for example, a loop as expected:
{% for object in object_list %}
{% partial test-partial %}
{% endfor %}
If you need to adjust the context, use the with tag as normal:
Rendering a partial — say a pagination widget — may be computationally expensive.
It's out-of-scope for django-template-partials to capture the generated HTML
to the context, but other options exist, such as the Slipper's library
fragment tag,
that allows exactly this behaviour.
Adding partials to template builtins.
Maybe you don't want to load the partials tags in every template…
{% load partials %}
The Django Template Language's OPTIONS
allow you to add to the builtins that are loaded for every template. You can
add the partials tags there:
If the NAME isn't provided, the penultimate element of the BACKEND value is
used - for example, "django.template.backends.django.DjangoTemplates" would
be equivalent to a NAME of "django".
Under the hood, wrap_loaders() is equivalent to explicitly defining the
loaders by-hand. Assuming defaults…