#582 closed (fixed)
[patch] Load templates from application eggs
Reported by: | Owned by: | Adrian Holovaty | |
---|---|---|---|
Component: | Template system | Version: | |
Severity: | normal | Keywords: | apps eggs templates |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
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 (6)
Change History (16)
by , 19 years ago
Attachment: | template_eggs.patch added |
---|
by , 19 years ago
Attachment: | template_eggs-newfiles.tar added |
---|
comment:1 by , 19 years ago
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.
comment:2 by , 19 years ago
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
.
by , 19 years ago
Attachment: | template_eggs-2.patch added |
---|
TEMPLATE_SOURCE_LOADERS moved into global_settings
comment:3 by , 19 years ago
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.
by , 19 years ago
Attachment: | template_eggs-3.patch added |
---|
comment:4 by , 19 years ago
Status: | new → 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?
comment:5 by , 19 years ago
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.
comment:6 by , 19 years ago
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?
comment:7 by , 19 years ago
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.
comment:8 by , 19 years ago
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."
- If
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.
comment:9 by , 19 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
comment:10 by , 18 years ago
Type: | enhancement |
---|
New unittests and testapp egg