Ticket #6587: new_template_lib_loader_6.diff

File new_template_lib_loader_6.diff, 4.5 KB (added by oyvind, 16 years ago)

populate app_label in templatetags/init..py, import_library is own function

  • django/template/__init__.py

    diff --git a/django/template/__init__.py b/django/template/__init__.py
    index 29daa76..3e48bb5 100644
    a b from django.utils.encoding import smart_unicode, force_unicode  
    5959from django.utils.translation import ugettext as _
    6060from django.utils.safestring import SafeData, EscapeData, mark_safe, mark_for_escaping
    6161from django.utils.html import escape
     62from django.templatetags import get_app_label_and_modules
    6263
    6364__all__ = ('Template', 'Context', 'RequestContext', 'compile_string')
    6465
    class Library(object):  
    910911            return func
    911912        return dec
    912913
    913 def get_library(module_name):
    914     lib = libraries.get(module_name, None)
     914def import_library(module_name):
     915    try:
     916        mod = __import__(module_name, {}, {}, [''])
     917    except ImportError:
     918        return None
     919    try:
     920        return mod.register
     921    except AttributeError:
     922        raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % module_name)
     923
     924def get_library(library_name):
     925    lib = libraries.get(library_name, None)
    915926    if not lib:
     927
     928        "Allow both library_name and app_label.library_name"
    916929        try:
    917             mod = __import__(module_name, {}, {}, [''])
    918         except ImportError, e:
    919             raise InvalidTemplateLibrary("Could not load template library from %s, %s" % (module_name, e))
    920         try:
    921             lib = mod.register
    922             libraries[module_name] = lib
    923         except AttributeError:
    924             raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % module_name)
     930            app, library = library_name.split('.')
     931        except:
     932            app, library = ('', library_name)
     933
     934        templatetags_modules = get_app_label_and_modules()
     935        tried_modules = []
     936        for module, app_label in templatetags_modules:
     937            if not app or app == app_label:
     938                module_name = '%s.%s' % (module, library)
     939                tried_modules.append(module_name)
     940                lib = import_library(module_name)
     941                if lib:
     942                    libraries[library_name] = lib
     943                    break
     944        if not lib:
     945            raise InvalidTemplateLibrary("Template library %s not found, tried %s" % (library_name, str(tried_modules)))
    925946    return lib
    926947
    927948def add_to_builtins(module_name):
    928     builtins.append(get_library(module_name))
     949    builtins.append(import_library(module_name))
    929950
    930951add_to_builtins('django.template.defaulttags')
    931952add_to_builtins('django.template.defaultfilters')
  • django/template/defaulttags.py

    diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
    index e5a8e66..cd671b9 100644
    a b def load(parser, token):  
    850850    for taglib in bits[1:]:
    851851        # add the library to the parser
    852852        try:
    853             lib = get_library("django.templatetags.%s" % taglib)
     853            lib = get_library(taglib)
    854854            parser.add_library(lib)
    855855        except InvalidTemplateLibrary, e:
    856856            raise TemplateSyntaxError("'%s' is not a valid tag library: %s" %
  • django/templatetags/__init__.py

    diff --git a/django/templatetags/__init__.py b/django/templatetags/__init__.py
    index 9204535..321ed98 100644
    a b  
    11from django.conf import settings
     2import os
    23
    3 for a in settings.INSTALLED_APPS:
    4     try:
    5         __path__.extend(__import__(a + '.templatetags', {}, {}, ['']).__path__)
    6     except ImportError:
    7         pass
     4templatetags_modules = []
     5
     6def get_app_label_and_modules():
     7    if not templatetags_modules:
     8        for a in ['django'] + list(settings.INSTALLED_APPS):
     9            try:
     10                module, app_label = (a, a.find('.')!=-1 and a.split('.')[-1] or a)
     11                name = module + '.templatetags'
     12                mod = __import__(name, {}, {}, [''])
     13                templatetags_modules.append((name, app_label))
     14            except ImportError:
     15                pass
     16    return templatetags_modules
  • tests/regressiontests/templates/tests.py

    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):  
    4545
    4646register.tag("echo", do_echo)
    4747
    48 template.libraries['django.templatetags.testtags'] = register
     48template.libraries['testtags'] = register
    4949
    5050#####################################
    5151# Helper objects for template tests #
Back to Top