Package: django-template-partials
django-template-partialsReusable named inline partials for the Django Template Language.Watch the talkI introduceddjango-template-partialsin my DjangoCon Europe 2023 talk in Edinburgh.For a quick introduction, you can watch the video on YouTube. 🍿InstallationInstall with pip:pipinstalldjango-template-partialsThen add toINSTALLED_APPSand you're good go.INSTALLED_APPS=["template_partials",...,]SeeAdvanced configuration (below)for
more options.Please see theCHANGELOGif you are upgrading from a previous version.Basic UsageOnce installed, load thepartialstags and define a re-usable partial at the top of your template:{% load partials %}

{% partialdef test-partial %}
TEST-PARTIAL-CONTENT
{% endpartialdef %}Fragment Re-useWith 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 loaderdjango-template-partialsis 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 theincludetag:{%include"example.html#test-partial"%}Outputting inlineYou might want to wrap an existing part of your page, and continue rendering
the content inside your partial, use theinlineargument in that situation:{% block main %}
{% partialdef inline-partial inline=True %}
CONTENT
{% endpartialdef %}
{% endblock main %}Controlling the contextA template partial is rendered with the current context.This means it works in, for example, a loop as expected:{%forobjectinobject_list%}{%partialtest-partial%}{%endfor%}If you need to adjust the context, use thewithtag as normal:{%withname=valueothername=othervalue%}{%partialtest-partial%}{%endwith%}Capturing outputRendering a partial — say a pagination widget — may be computationally expensive.It's out-of-scope fordjango-template-partialsto capture the generated HTML
to the context, but other options exist, such as theSlipper'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…{%loadpartials%}TheDjango Template Language's OPTIONSallow you to add to thebuiltinsthat are loaded for every template. You can
add the partials tags there:OPTIONS = {
    "builtins": ["template_partials.templatetags.partials"],
}That's the basics. Enjoy! 🚀Advanced configurationBy default, adding"template_partials"to yourINSTALLED_APPSwill
configure any Django template backend to use the partials template loader.If you need to control this behaviour, you can use an alternativeSimpleAppConfig, whichwill notadjust yourTEMPLATESsetting:INSTALLED_APPS=["template_partials.apps.SimpleAppConfig",...,]If you useSimpleAppConfig, you will need to configure the template loader yourself.Awrap_loaders()function is available, and can be used to configure any
specific template engine instance with the template partials loader.You can use the backend'sNAMEtowrap_loaders()to add the partial loader just for that backend:fromtemplate_partials.appsimportwrap_loadersTEMPLATES=[...,{"BACKEND":"...","NAME":"myname","OPTIONS":{...,},},...,]wrap_loaders("myname")If theNAMEisn't provided, the penultimate element of theBACKENDvalue is
used - for example,"django.template.backends.django.DjangoTemplates"would
be equivalent to aNAMEof"django".Under the hood,wrap_loaders()is equivalent to explicitly defining theloadersby-hand. Assuming defaults…fromdjango.confimportsettingsdefault_loaders=["django.template.loaders.filesystem.Loader","django.template.loaders.app_directories.Loader",]cached_loaders=[("django.template.loaders.cached.Loader",default_loaders)]partial_loaders=[("template_partials.loader.Loader",cached_loaders)]settings.TEMPLATES[...]['OPTIONS']['loaders']=partial_loaders… whereTEMPLATES[...]is the entry inTEMPLATESwith theNAMEmatching
that passed towrap_loaders().Running the testsFork, then clone the repo:gitclonegit@github.com:your-username/django-template-partials.gitSet up a venv:python-mvenv.venvsource.venv/bin/activate
python-mpipinstall-e.[tests]Then you can run the tests with thejustcommand runner:justtestOr with coverage:justcoverageIf you don't havejustinstalled, you can look in thejustfilefor a
commands that are run.