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
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.template import TemplateDoesNotExist
|
|
|
|
from django.conf import settings
|
2005-10-15 06:22:12 +08:00
|
|
|
|
2005-10-17 09:53:22 +08:00
|
|
|
def load_template_source(template_name, template_dirs=None):
|
2005-10-15 06:22:12 +08:00
|
|
|
"""
|
|
|
|
Loads templates from Python eggs via pkg_resource.resource_string.
|
|
|
|
|
2005-10-17 09:53:22 +08:00
|
|
|
For every installed app, it tries to get the resource (app, template_name).
|
2005-10-15 06:22:12 +08:00
|
|
|
"""
|
|
|
|
if resource_string is not None:
|
2006-05-02 09:31:56 +08:00
|
|
|
pkg_name = 'templates/' + template_name
|
|
|
|
for app in settings.INSTALLED_APPS:
|
2005-10-15 06:22:12 +08:00
|
|
|
try:
|
2008-05-29 21:11:23 +08:00
|
|
|
return (resource_string(app, pkg_name).decode(settings.FILE_CHARSET), 'egg:%s:%s' % (app, pkg_name))
|
2005-10-15 06:22:12 +08:00
|
|
|
except:
|
|
|
|
pass
|
2005-10-17 09:53:22 +08:00
|
|
|
raise TemplateDoesNotExist, template_name
|
2005-10-15 06:22:12 +08:00
|
|
|
load_template_source.is_usable = resource_string is not None
|