| 1 | # Wrapper for loading templates from eggs via pkg_resources.resource_string.
|
|---|
| 2 |
|
|---|
| 3 | import os
|
|---|
| 4 |
|
|---|
| 5 | from django.conf.settings import INSTALLED_APPS
|
|---|
| 6 | from django.core.template import TemplateDoesNotExist
|
|---|
| 7 |
|
|---|
| 8 | def load_template_source(name, dirs=None):
|
|---|
| 9 | """
|
|---|
| 10 | This searches the application directories for a given
|
|---|
| 11 | template. It looks into "templates" subdirectories of
|
|---|
| 12 | any installed applications.
|
|---|
| 13 | """
|
|---|
| 14 | for app in INSTALLED_APPS:
|
|---|
| 15 | i = app.rfind('.')
|
|---|
| 16 | m, a = app[:i], app[i+1:]
|
|---|
| 17 | m = getattr(__import__(m, globals(), locals(), [a]), a)
|
|---|
| 18 | app_root = os.path.dirname(m.__file__)
|
|---|
| 19 | td = os.path.join(app_root, 'templates')
|
|---|
| 20 | if os.path.isdir(td):
|
|---|
| 21 | tn = os.path.join(tn, '%s.html' % name)
|
|---|
| 22 | if os.path.exist(tn):
|
|---|
| 23 | return open(tn).read()
|
|---|
| 24 | raise TemplateDoesNotExist, name
|
|---|
| 25 |
|
|---|
| 26 | load_template_source.is_usable = True
|
|---|