diff -r 6eb3c3a60842 django/template/__init__.py --- a/django/template/__init__.py Mon Jan 25 12:29:56 2010 +0000 +++ b/django/template/__init__.py Tue Jan 26 00:06:22 2010 +0800 @@ -49,6 +49,7 @@ u'' """ import re +import imp from inspect import getargspec from django.conf import settings @@ -809,7 +810,7 @@ def render(self, context): return self.s - + def _render_value_in_context(value, context): """ Converts any value to a string to become part of a rendered template. This @@ -966,22 +967,67 @@ return func return dec -def get_library(module_name): - lib = libraries.get(module_name, None) +def import_library(taglib_module): + """Load a template tag library module. + + Verifies that the library contains a 'register' attribute, and + returns that attribute as the representation of the library + """ + try: + mod = import_module(taglib_module) + except ImportError: + return None + try: + return mod.register + except AttributeError: + raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % taglib_module) + +templatetags_modules= [] + +def get_templatetags_modules(): + """Return the list of all available template tag modules. + + Caches the result for faster access. + """ + if not templatetags_modules: + # Populate list once per thread. + for app_module in ['django'] + list(settings.INSTALLED_APPS): + try: + templatetag_module = '%s.templatetags' % app_module + import_module(templatetag_module) + templatetags_modules.append(templatetag_module) + except ImportError: + continue + return templatetags_modules + +def get_library(library_name): + """ + Load the template library module with the given name. + + If library is not already loaded loop over all templatetags modules to locate it. + + {% load somelib %} and {% load someotherlib %} loops twice. + + Subsequent loads eg. {% load somelib %} in the same thread will grab the cached + module from libraries. + """ + lib = libraries.get(library_name, None) if not lib: - try: - mod = import_module(module_name) - except ImportError, e: - raise InvalidTemplateLibrary("Could not load template library from %s, %s" % (module_name, e)) - try: - lib = mod.register - libraries[module_name] = lib - except AttributeError: - raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % module_name) + templatetags_modules = get_templatetags_modules() + tried_modules = [] + for module in templatetags_modules: + taglib_module = str('%s.%s' % (module, library_name)) + tried_modules.append(taglib_module) + lib = import_library(taglib_module) + if lib: + libraries[library_name] = lib + break + if not lib: + raise InvalidTemplateLibrary("Template library %s not found, tried %s" % (library_name, ','.join(tried_modules))) return lib -def add_to_builtins(module_name): - builtins.append(get_library(module_name)) +def add_to_builtins(module): + builtins.append(import_library(module)) add_to_builtins('django.template.defaulttags') add_to_builtins('django.template.defaultfilters') diff -r 6eb3c3a60842 django/template/defaulttags.py --- a/django/template/defaulttags.py Mon Jan 25 12:29:56 2010 +0000 +++ b/django/template/defaulttags.py Tue Jan 26 00:06:22 2010 +0800 @@ -920,7 +920,7 @@ for taglib in bits[1:]: # add the library to the parser try: - lib = get_library("django.templatetags.%s" % taglib) + lib = get_library(taglib) parser.add_library(lib) except InvalidTemplateLibrary, e: raise TemplateSyntaxError("'%s' is not a valid tag library: %s" % diff -r 6eb3c3a60842 django/templatetags/__init__.py --- a/django/templatetags/__init__.py Mon Jan 25 12:29:56 2010 +0000 +++ b/django/templatetags/__init__.py Tue Jan 26 00:06:22 2010 +0800 @@ -1,8 +0,0 @@ -from django.conf import settings -from django.utils import importlib - -for a in settings.INSTALLED_APPS: - try: - __path__.extend(importlib.import_module('.templatetags', a).__path__) - except ImportError: - pass diff -r 6eb3c3a60842 tests/regressiontests/templates/tests.py --- a/tests/regressiontests/templates/tests.py Mon Jan 25 12:29:56 2010 +0000 +++ b/tests/regressiontests/templates/tests.py Tue Jan 26 00:06:22 2010 +0800 @@ -59,7 +59,7 @@ register.tag("echo", do_echo) -template.libraries['django.templatetags.testtags'] = register +template.libraries['testtags'] = register ##################################### # Helper objects for template tests #