Django

Code

Ticket #582 (closed: fixed)

Opened 3 years ago

Last modified 2 years ago

[patch] Load templates from application eggs

Reported by: sune.kirkeby@gmail.com Assigned to: adrian
Milestone: Component: Template system
Version: Keywords: apps eggs templates
Cc: Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

The attached patch changes django.core.template_loader to try several different template-source-loaders; first it tries the vanilla django.core.template_file loader; then it tries the new django.core.template_eggs loader. The new loader tries to load the named template from 'templates/%s' % name for each installed app. So one can package an app as a python egg and distribute it as one file with default templates included, and installation is as simple as dropping it somewhere in sys.path :)

Attachments

template_eggs.patch (1.8 kB) - added by sune.kirkeby@gmail.com on 10/01/05 04:11:08.
template_eggs-newfiles.tar (10.0 kB) - added by sune.kirkeby@gmail.com on 10/01/05 04:12:19.
New unittests and testapp egg
template_eggs-runtests.patch (1.3 kB) - added by sune.kirkeby@gmail.com on 10/01/05 04:12:47.
Add app-egg to test environment
template_eggs-2.patch (2.6 kB) - added by sune.kirkeby@gmail.com on 10/04/05 04:37:20.
TEMPLATE_SOURCE_LOADERS moved into global_settings
template_eggs-3.patch (3.1 kB) - added by sune.kirkeby@gmail.com on 10/04/05 05:05:43.
template_eggs-4.patch (3.1 kB) - added by sune.kirkeby@gmail.com on 10/06/05 03:56:15.
Python 2.3 safe patch

Change History

10/01/05 04:11:08 changed by sune.kirkeby@gmail.com

  • attachment template_eggs.patch added.

10/01/05 04:12:19 changed by sune.kirkeby@gmail.com

  • attachment template_eggs-newfiles.tar added.

New unittests and testapp egg

10/01/05 04:12:47 changed by sune.kirkeby@gmail.com

  • attachment template_eggs-runtests.patch added.

Add app-egg to test environment

10/02/05 13:21:30 changed by adrian

This is an interesting idea! However, I suspect most people aren't going to use eggs to store their templates, so I think this should be an opt-in thing. I don't want the extra "look for templates in eggs" overhead to affect people who don't use eggs. As in #583, a good compromise might be to introduce an opt-in setting: ALLOW_TEMPLATES_IN_EGGS, perhaps? Or we could move the template_source_loaders into settings, so that people have the flexibility to define their own template loaders.

10/04/05 04:36:33 changed by anonymous

However, I suspect most people aren't going to use eggs to store their templates, so I think this should be an opt-in thing. I don't want the extra "look for templates in eggs" overhead to affect people who don't use eggs.

I'm not sure there is any noticable performance penalty here, after all the template_eggs loader is only invoked if the template is not found in any of TEMPLATE_DIRS. So, for people who do not use eggs, it will only execute if they try to load a non-existing template.

But, I will attach a new patch, which moves TEMPLATE_SOURCE_LOADERS into global_settings.

10/04/05 04:37:20 changed by sune.kirkeby@gmail.com

  • attachment template_eggs-2.patch added.

TEMPLATE_SOURCE_LOADERS moved into global_settings

10/04/05 05:04:47 changed by sune.kirkeby@gmail.com

Mental note to self: I should not touch code before the third cup of coffee. Please ignore the -2.patch, it's broken in several dumb ways. The -3.patch works for me, and passes the unittests.

10/04/05 05:05:43 changed by sune.kirkeby@gmail.com

  • attachment template_eggs-3.patch added.

10/06/05 03:56:15 changed by sune.kirkeby@gmail.com

  • attachment template_eggs-4.patch added.

Python 2.3 safe patch

10/13/05 23:45:46 changed by adrian

  • status changed from new to assigned.

Another question: As the patch stands, template_eggs.load_template_source() will fail silently (raising TemplateDoesNotExist) if resource_string() isn't available. Should it instead raise ImproperlyConfigured to tell the Django admin to remove django.core.template_eggs.load_template_source from the TEMPLATE_SOURCE_LOADERS setting?

10/13/05 23:48:02 changed by adrian

A follow-up to my previous comment. If indeed we changed template_eggs.load_template_source() to raise ImproperlyConfigured, we'd have to remove template_eggs.load_template_source from TEMPLATE_SOURCE_LOADERS in global_settings. Which would mean people would have to manually activate it.

10/14/05 01:52:20 changed by Sune Kirkeby <sune.kirkeby@gmail.com>

Silent failure are nasty critters; on the other hand, I like the all batteries included'ness of having it in global_settings. Maybe we could issue a warning if importing pkg_resources fails?

10/14/05 02:12:06 changed by hugo

couldn't django.conf.settings fix the TEMPLATE_SOURCE_LOADERS if resource_string isn't available? It could issue a warning that it is about to remove incompatible loader stuff and then go on. Or make the template source loaders "intelligent" with a "is_useable" hook - the init stuff of django tries to load the TEMPLATE_SOURCE_LOADERS and check their is_useable() hook and if that returns False, doesn't load it. That way template loaders could be added for stuff that's optional in the python environment but could still be listed in the global settings to give the "batteries included" feeling.

10/14/05 08:57:12 changed by adrian

The is_usable() hook idea is a good one, combined with the concept of throwing a warning at the init stage.

How's this for an algorithm:

  • For each loader in TEMPLATE_SOURCE_LOADERS:
    • If loader.is_usable()
      • Register the loader
    • Else
      • Throw a warning "Loader isn't available. You might as well remove it from TEMPLATE_SOURCE_LOADERS if you're not using it."

I've made some of these changes to my local copy of Sune's patch, so there's no need to add another patch. I'll probably be checking it in later today.

10/14/05 17:22:13 changed by adrian

  • status changed from assigned to closed.
  • resolution set to fixed.

(In [870]) Fixed #582 -- Added support for loading templates from Python eggs, and a TEMPLATE_LOADERS setting, which defines which loaders to use. Thanks, Sune

06/12/06 06:55:36 changed by Here

  • type deleted.

Add/Change #582 ([patch] Load templates from application eggs)




Change Properties
Action