2005-10-15 06:22:12 +08:00
|
|
|
# Wrapper for loading templates from eggs via pkg_resources.resource_string.
|
|
|
|
|
|
|
|
try:
|
|
|
|
from pkg_resources import resource_string
|
|
|
|
except ImportError:
|
|
|
|
resource_string = None
|
|
|
|
|
2010-11-27 13:47:30 +08:00
|
|
|
from django.template.base import TemplateDoesNotExist
|
2009-12-14 20:08:23 +08:00
|
|
|
from django.template.loader import BaseLoader
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.conf import settings
|
2005-10-15 06:22:12 +08:00
|
|
|
|
2009-12-14 20:08:23 +08:00
|
|
|
class Loader(BaseLoader):
|
|
|
|
is_usable = resource_string is not None
|
|
|
|
|
|
|
|
def load_template_source(self, template_name, template_dirs=None):
|
|
|
|
"""
|
|
|
|
Loads templates from Python eggs via pkg_resource.resource_string.
|
|
|
|
|
|
|
|
For every installed app, it tries to get the resource (app, template_name).
|
|
|
|
"""
|
|
|
|
if resource_string is not None:
|
|
|
|
pkg_name = 'templates/' + template_name
|
|
|
|
for app in settings.INSTALLED_APPS:
|
|
|
|
try:
|
|
|
|
return (resource_string(app, pkg_name).decode(settings.FILE_CHARSET), 'egg:%s:%s' % (app, pkg_name))
|
|
|
|
except:
|
|
|
|
pass
|
2010-01-11 02:36:20 +08:00
|
|
|
raise TemplateDoesNotExist(template_name)
|
2005-10-15 06:22:12 +08:00
|
|
|
|
2009-12-14 20:08:23 +08:00
|
|
|
_loader = Loader()
|