Opened 4 years ago

Closed 11 months ago

#17093 closed Cleanup/optimization (fixed)

Refactor django.template to quarantine global state

Reported by: carljm Owned by: aaugustin
Component: Template system Version: master
Severity: Normal Keywords:
Cc: bradley.ayers@…, waylan@…, techtonik@…, kmike84@…, tgecho, yac Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


The template engine keeps a lot of global state around: the instantiated template loaders, the list of builtins, the cached list of loaded templatetag modules. This makes it inflexible for use as a library (you can't render different templates using different template loaders or directories or tag modules in the same process) and makes test isolation very difficult (for the same reasons).

This can be fixed via a refactoring to introduce a TemplateEngine class to encapsulate all this state. We would still create a default module-level instance of TemplateEngine based on the settings, and backwards-compatibility wrappers for the current public API that delegate to the default engine. But this would then allow alternative instances of TemplateEngine to be created and used for library use and testing, that would be self-contained and independent from settings.

In the long run, this type of refactoring is also necessary if we want to move towards "app-object" style configuration in place of global settings.

Change History (18)

comment:1 Changed 4 years ago by Alex

  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 4 years ago by Bradley Ayers <bradley.ayers@…>

  • Cc bradley.ayers@… added

comment:3 Changed 4 years ago by wayla

  • Cc waylan@… added

comment:4 Changed 4 years ago by steveire

For an equivalent API (in C++) see

It also allows the case of pre-loading template tag modules brought up recently on the devel mailing list.

comment:5 Changed 4 years ago by anatoly techtonik <techtonik@…>

  • Cc techtonik@… added

comment:6 Changed 4 years ago by kmike

  • Cc kmike84@… added

comment:7 Changed 4 years ago by tgecho

  • Cc tgecho added

comment:8 Changed 3 years ago by yac

  • Cc yac added

comment:9 Changed 3 years ago by chrismedrela

  • Owner changed from nobody to chrismedrela

comment:10 Changed 3 years ago by anonymous

comment:11 Changed 3 years ago by chrismedrela

That was me.

comment:12 Changed 3 years ago by chrismedrela

I've started discussion at django-developers mailing list

comment:13 Changed 3 years ago by chrismedrela

I rebased my work. I kept old branch and created new one ( where I'm pushing my work. The branch is almost ready to review.

comment:14 Changed 3 years ago by chrismedrela

comment:15 Changed 3 years ago by techtonik

What is the scope of data that should encapsulated in TemplateEngine exactly, and what will be left behind? It is not 100% clear from the description, so I can't say if it will solve my problem with #17368.

Last edited 3 years ago by techtonik (previous) (diff)

comment:16 Changed 11 months ago by aaugustin

  • Owner changed from chrismedrela to aaugustin
  • Status changed from new to assigned

I'm picking this up as part of my Multiple Template Engines project.


I'm going to perform a more intensive cleanup and reorganisation of django.template.

comment:18 Changed 11 months ago by timgraham

  • Resolution set to fixed
  • Status changed from assigned to closed

I assume this can be closed since the PR was committed. Please reopen if there is still more work to do.

Note: See TracTickets for help on using tickets.
Back to Top