Opened 12 years ago

Closed 9 years 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: dev
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 by Alex Gaynor, 12 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Bradley Ayers <bradley.ayers@…>, 12 years ago

Cc: bradley.ayers@… added

comment:3 by Waylan Limberg, 12 years ago

Cc: waylan@… added

comment:4 by Stephen Kelly, 12 years ago

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 by anatoly techtonik <techtonik@…>, 12 years ago

Cc: techtonik@… added

comment:6 by Mikhail Korobov, 12 years ago

Cc: kmike84@… added

comment:7 by tgecho, 12 years ago

Cc: tgecho added

comment:8 by yac, 12 years ago

Cc: yac added

comment:9 by chrismedrela, 11 years ago

Owner: changed from nobody to chrismedrela

comment:11 by chrismedrela, 11 years ago

That was me.

comment:12 by chrismedrela, 11 years ago

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

comment:13 by chrismedrela, 11 years ago

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:15 by anatoly techtonik, 11 years ago

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 11 years ago by anatoly techtonik (previous) (diff)

comment:16 by Aymeric Augustin, 9 years ago

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 by Aymeric Augustin, 9 years ago

Has patch: set

comment:18 by Tim Graham, 9 years ago

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