Refactor django.template to quarantine global state
|Reported by:||carljm||Owned by:||chrismedrela|
|Cc:||bradley.ayers@…, waylan@…, techtonik@…, kmike84@…, tgecho, yac||Triage Stage:||Accepted|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||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.