Ticket #6587: new_template_lib_loader_7.diff

File new_template_lib_loader_7.diff, 5.0 KB (added by oyvind, 17 years ago)

better naming of variables, some code fixes, added some docstrings

  • django/template/__init__.py

    diff --git a/django/template/__init__.py b/django/template/__init__.py
    index e60ff64..65880ae 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):  
    913914            return func
    914915        return dec
    915916
    916 def get_library(module_name):
    917     lib = libraries.get(module_name, None)
     917def 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
     927def get_library(library_name):
     928    lib = libraries.get(library_name, None)
    918929    if not lib:
     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
    919940        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)
     941            """ Allow both {% load library_name %} and {% load app_label.library_name %} """
     942            app, library = library_name.split('.')
     943        except ValueError:
     944            app, library = ('', library_name)
     945
     946        templatetags_modules = get_app_label_and_modules()
     947        tried_modules = []
     948        for module, app_label in templatetags_modules:
     949            """ If using app_label.library check that app_label is the same as app. """
     950            if not app or app == app_label:
     951                module_name = '%s.%s' % (module, library)
     952                tried_modules.append(module_name)
     953                lib = import_library(module_name)
     954                if lib:
     955                    libraries[library_name] = lib
     956                    break
     957        if not lib:
     958            raise InvalidTemplateLibrary("Template library %s not found, tried %s" % (library_name, str(tried_modules)))
    928959    return lib
    929960
    930961def add_to_builtins(module_name):
    931     builtins.append(get_library(module_name))
     962    builtins.append(import_library(module_name))
    932963
    933964add_to_builtins('django.template.defaulttags')
    934965add_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..28aa112 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
     4app_labels_and_modules= []
     5
     6def get_app_label_and_modules():
     7    if not app_labels_and_modules:
     8        """ Populate list once per thread. """
     9        for a in ['django'] + list(settings.INSTALLED_APPS):
     10            try:
     11                module, app_label = (a, a.split('.')[-1])
     12                name = module + '.templatetags'
     13                mod = __import__(name, {}, {}, [''])
     14                app_labels_and_modules.append((name, app_label))
     15            except ImportError:
     16                pass
     17    return app_labels_and_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