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/template/defaulttags.py b/django/template/defaulttags.py
index e5a8e66..cd671b9 100644
a
|
b
|
def load(parser, token):
|
850 | 850 | for taglib in bits[1:]: |
851 | 851 | # add the library to the parser |
852 | 852 | try: |
853 | | lib = get_library("django.templatetags.%s" % taglib) |
| 853 | lib = get_library(taglib) |
854 | 854 | parser.add_library(lib) |
855 | 855 | except InvalidTemplateLibrary, e: |
856 | 856 | raise TemplateSyntaxError("'%s' is not a valid tag library: %s" % |
diff --git a/django/templatetags/__init__.py b/django/templatetags/__init__.py
index 9204535..f80c664 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 | path = mod.__path__[0] |
| 16 | for root, dirs, files in os.walk(path): |
| 17 | if root == path: |
| 18 | for file in files: |
| 19 | if not file.endswith('.pyc') and not file.startswith('__init__'): |
| 20 | library_name = os.path.splitext(file)[0] |
| 21 | module_path = '%s.%s' % (name, library_name) |
| 22 | if library_name not in library_names: |
| 23 | library_names[library_name] = module_path |
| 24 | library_names[appname + '.' + library_name] = module_path |
| 25 | except ImportError: |
| 26 | pass |
| 27 | return library_names |
diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
index aef6f50..0490f2a 100644
a
|
b
|
def do_echo(parser, token):
|
45 | 45 | |
46 | 46 | register.tag("echo", do_echo) |
47 | 47 | |
48 | | template.libraries['django.templatetags.testtags'] = register |
| 48 | template.libraries['testtags'] = register |
49 | 49 | |
50 | 50 | ##################################### |
51 | 51 | # Helper objects for template tests # |