Opened 5 years ago

Closed 22 months ago

#17093 closed Cleanup/optimization (fixed)

Refactor django.template to quarantine global state

Reported by: Carl Meyer Owned by: Aymeric Augustin
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

Description

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 5 years ago by Alex Gaynor

Triage Stage: UnreviewedAccepted

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

Cc: bradley.ayers@… added

comment:3 Changed 5 years ago by Waylan Limberg

Cc: waylan@… added

comment:4 Changed 5 years ago by Stephen Kelly

For an equivalent API (in C++) see http://www.grantlee.org/apidox/classGrantlee_1_1Engine.html.

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

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

Cc: techtonik@… added

comment:6 Changed 5 years ago by Mikhail Korobov

Cc: kmike84@… added

comment:7 Changed 5 years ago by tgecho

Cc: tgecho added

comment:8 Changed 4 years ago by yac

Cc: yac added

comment:9 Changed 4 years ago by chrismedrela

Owner: changed from nobody to chrismedrela

comment:10 Changed 4 years ago by anonymous

comment:11 Changed 4 years ago by chrismedrela

That was me.

comment:12 Changed 4 years ago by chrismedrela

I've started discussion at django-developers mailing list https://groups.google.com/d/topic/django-developers/WT1cOvpPbSM/discussion.

comment:13 Changed 4 years ago by chrismedrela

I rebased my work. I kept old branch and created new one (https://github.com/chrismedrela/django/tree/ticket_17093v2) where I'm pushing my work. The branch is almost ready to review.

comment:14 Changed 4 years ago by chrismedrela

comment:15 Changed 4 years ago by anatoly 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 4 years ago by anatoly techtonik (previous) (diff)

comment:16 Changed 23 months ago by Aymeric Augustin

Owner: changed from chrismedrela to Aymeric Augustin
Status: newassigned

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

See https://github.com/aaugustin/django/compare/refactor-template-engine-as-library

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

comment:17 Changed 23 months ago by Aymeric Augustin

Has patch: set

comment:18 Changed 22 months ago by Tim Graham

Resolution: fixed
Status: assignedclosed

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