diff --git a/django/template/__init__.py b/django/template/__init__.py
index 29daa76..ad0e6da 100644
a
|
b
|
from django.utils.encoding import smart_unicode, force_unicode
|
59 | 59 | from django.utils.translation import ugettext as _ |
60 | 60 | from django.utils.safestring import SafeData, EscapeData, mark_safe, mark_for_escaping |
61 | 61 | from django.utils.html import escape |
| 62 | from django.templatetags import get_library_names |
62 | 63 | |
63 | 64 | __all__ = ('Template', 'Context', 'RequestContext', 'compile_string') |
64 | 65 | |
… |
… |
class Library(object):
|
910 | 911 | return func |
911 | 912 | return dec |
912 | 913 | |
913 | | def get_library(module_name): |
914 | | lib = libraries.get(module_name, None) |
| 914 | def get_library(library_name, full_path=False): |
| 915 | lib = libraries.get(library_name, None) |
915 | 916 | if not lib: |
| 917 | library_names = get_library_names() |
916 | 918 | try: |
| 919 | module_name = full_path and library_name or library_names[library_name] |
| 920 | except KeyError, e: |
| 921 | raise InvalidTemplateLibrary("No such library %s, %s" % (library_name, e)) |
| 922 | try: |
| 923 | module_name = full_path and library_name or library_names[library_name] |
917 | 924 | mod = __import__(module_name, {}, {}, ['']) |
918 | 925 | except ImportError, e: |
919 | 926 | raise InvalidTemplateLibrary("Could not load template library from %s, %s" % (module_name, e)) |
920 | 927 | try: |
921 | 928 | lib = mod.register |
922 | | libraries[module_name] = lib |
| 929 | libraries[library_name] = lib |
923 | 930 | except AttributeError: |
924 | 931 | raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % module_name) |
925 | 932 | return lib |
926 | 933 | |
927 | 934 | def add_to_builtins(module_name): |
928 | | builtins.append(get_library(module_name)) |
| 935 | builtins.append(get_library(module_name, full_path=True)) |
929 | 936 | |
930 | 937 | add_to_builtins('django.template.defaulttags') |
931 | 938 | add_to_builtins('django.template.defaultfilters') |
diff --git a/django/templatetags/__init__.py b/django/templatetags/__init__.py
index 9204535..deb6108 100644
a
|
b
|
|
1 | 1 | from django.conf import settings |
| 2 | import os |
2 | 3 | |
3 | | for a in settings.INSTALLED_APPS: |
4 | | try: |
5 | | __path__.extend(__import__(a + '.templatetags', {}, {}, ['']).__path__) |
6 | | except ImportError: |
7 | | pass |
| 4 | library_names = {} |
| 5 | |
| 6 | def get_library_names(): |
| 7 | if not library_names: |
| 8 | library_names['i18n'] = 'django.templatetags.i18n' |
| 9 | library_names['cache'] = 'django.templatetags.cache' |
| 10 | for a in settings.INSTALLED_APPS: |
| 11 | try: |
| 12 | appname = a.find('.')!=-1 and a.split('.')[-1] or a |
| 13 | name = a + '.templatetags' |
| 14 | mod = __import__(name, {}, {}, ['']) |
| 15 | for root, dirs, files in os.walk(mod.__path__[0]): |
| 16 | for file in files: |
| 17 | if not file.endswith('.pyc') and not file.startswith('__init__'): |
| 18 | library_name = os.path.splitext(file)[0] |
| 19 | if library_name not in library_names: |
| 20 | library_names[library_name] = name + '.' + library_name |
| 21 | library_names[appname + '.' + library_name] = name + '.' + library_name |
| 22 | except ImportError: |
| 23 | pass |
| 24 | return library_names |