diff --git a/django/template/__init__.py b/django/template/__init__.py
index 5c4ab30..1d61387 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_templatetags_modules |
| 62 | 63 | |
| 63 | 64 | __all__ = ('Template', 'Context', 'RequestContext', 'compile_string') |
| 64 | 65 | |
| … |
… |
class Library(object):
|
| 913 | 914 | return func |
| 914 | 915 | return dec |
| 915 | 916 | |
| 916 | | def get_library(module_name): |
| 917 | | lib = libraries.get(module_name, None) |
| | 917 | def import_library(module_name): |
| | 918 | try: |
| | 919 | mod = __import__(module_name, {}, {}, ['']) |
| | 920 | except ImportError: |
| | 921 | return None |
| | 922 | try: |
| | 923 | return mod.register |
| | 924 | except AttributeError: |
| | 925 | raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % module_name) |
| | 926 | |
| | 927 | def get_library(library_name): |
| | 928 | lib = libraries.get(library_name, None) |
| 918 | 929 | if not lib: |
| 919 | | try: |
| 920 | | mod = __import__(module_name, {}, {}, ['']) |
| 921 | | except ImportError, e: |
| 922 | | raise InvalidTemplateLibrary("Could not load template library from %s, %s" % (module_name, e)) |
| 923 | | try: |
| 924 | | lib = mod.register |
| 925 | | libraries[module_name] = lib |
| 926 | | except AttributeError: |
| 927 | | raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % module_name) |
| | 930 | |
| | 931 | """ |
| | 932 | If library is not already loaded loop over all templatetags modules to locate it. |
| | 933 | |
| | 934 | {% load somelib %} and {% load someotherlib %} loops twice. |
| | 935 | |
| | 936 | Subsequent loads eg. {% load somelib %} in the same thread will grab the cached |
| | 937 | module from libraries. |
| | 938 | """ |
| | 939 | templatetags_modules = get_templatetags_modules() |
| | 940 | tried_modules = [] |
| | 941 | for module in templatetags_modules: |
| | 942 | taglib_module = '%s.%s' % (module, library_name) |
| | 943 | tried_modules.append(taglib_module) |
| | 944 | lib = import_library(taglib_module) |
| | 945 | if lib: |
| | 946 | libraries[library_name] = lib |
| | 947 | break |
| | 948 | if not lib: |
| | 949 | raise InvalidTemplateLibrary("Template library %s not found, tried %s" % (library_name, ','.join(tried_modules))) |
| 928 | 950 | return lib |
| 929 | 951 | |
| 930 | 952 | def add_to_builtins(module_name): |
| 931 | | builtins.append(get_library(module_name)) |
| | 953 | builtins.append(import_library(module_name)) |
| 932 | 954 | |
| 933 | 955 | add_to_builtins('django.template.defaulttags') |
| 934 | 956 | add_to_builtins('django.template.defaultfilters') |
diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
index cf3b35b..665c30a 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..93a97b4 100644
|
a
|
b
|
|
| 1 | 1 | from django.conf import settings |
| 2 | 2 | |
| 3 | | for a in settings.INSTALLED_APPS: |
| 4 | | try: |
| 5 | | __path__.extend(__import__(a + '.templatetags', {}, {}, ['']).__path__) |
| 6 | | except ImportError: |
| 7 | | pass |
| | 3 | templatetags_modules= [] |
| | 4 | |
| | 5 | def get_templatetags_modules(): |
| | 6 | if not templatetags_modules: |
| | 7 | """ Populate list once per thread. """ |
| | 8 | for app_module in ['django'] + list(settings.INSTALLED_APPS): |
| | 9 | try: |
| | 10 | name = '%s.templatetags' % app_module |
| | 11 | mod = __import__(name, {}, {}, ['']) |
| | 12 | templatetags_modules.append(name) |
| | 13 | except ImportError: |
| | 14 | pass |
| | 15 | return templatetags_modules |
diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
index 4effea5..dc61a8a 100644
|
a
|
b
|
def do_echo(parser, token):
|
| 48 | 48 | |
| 49 | 49 | register.tag("echo", do_echo) |
| 50 | 50 | |
| 51 | | template.libraries['django.templatetags.testtags'] = register |
| | 51 | template.libraries['testtags'] = register |
| 52 | 52 | |
| 53 | 53 | ##################################### |
| 54 | 54 | # Helper objects for template tests # |