Ticket #6587: t6587-r12291.1.diff

File t6587-r12291.1.diff, 4.8 KB (added by Russell Keith-Magee, 14 years ago)

RC1 of template load refactoring

  • django/template/__init__.py

    diff -r 6eb3c3a60842 django/template/__init__.py
    a b  
    4949u'<html></html>'
    5050"""
    5151import re
     52import imp
    5253from inspect import getargspec
    5354
    5455from django.conf import settings
     
    809810
    810811    def render(self, context):
    811812        return self.s
    812    
     813
    813814def _render_value_in_context(value, context):
    814815    """
    815816    Converts any value to a string to become part of a rendered template. This
     
    966967            return func
    967968        return dec
    968969
    969 def get_library(module_name):
    970     lib = libraries.get(module_name, None)
     970def import_library(taglib_module):
     971    """Load a template tag library module.
     972
     973    Verifies that the library contains a 'register' attribute, and
     974    returns that attribute as the representation of the library
     975    """
     976    try:
     977        mod = import_module(taglib_module)
     978    except ImportError:
     979        return None
     980    try:
     981        return mod.register
     982    except AttributeError:
     983        raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % taglib_module)
     984
     985templatetags_modules= []
     986
     987def get_templatetags_modules():
     988    """Return the list of all available template tag modules.
     989
     990    Caches the result for faster access.
     991    """
     992    if not templatetags_modules:
     993        # Populate list once per thread.
     994        for app_module in ['django'] + list(settings.INSTALLED_APPS):
     995            try:
     996                templatetag_module = '%s.templatetags' % app_module
     997                import_module(templatetag_module)
     998                templatetags_modules.append(templatetag_module)
     999            except ImportError:
     1000                continue
     1001    return templatetags_modules
     1002
     1003def get_library(library_name):
     1004    """
     1005    Load the template library module with the given name.
     1006
     1007    If library is not already loaded loop over all templatetags modules to locate it.
     1008
     1009    {% load somelib %} and {% load someotherlib %} loops twice.
     1010
     1011    Subsequent loads eg. {% load somelib %} in the same thread will grab the cached
     1012    module from libraries.
     1013    """
     1014    lib = libraries.get(library_name, None)
    9711015    if not lib:
    972         try:
    973             mod = import_module(module_name)
    974         except ImportError, e:
    975             raise InvalidTemplateLibrary("Could not load template library from %s, %s" % (module_name, e))
    976         try:
    977             lib = mod.register
    978             libraries[module_name] = lib
    979         except AttributeError:
    980             raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % module_name)
     1016        templatetags_modules = get_templatetags_modules()
     1017        tried_modules = []
     1018        for module in templatetags_modules:
     1019            taglib_module = str('%s.%s' % (module, library_name))
     1020            tried_modules.append(taglib_module)
     1021            lib = import_library(taglib_module)
     1022            if lib:
     1023                libraries[library_name] = lib
     1024                break
     1025        if not lib:
     1026            raise InvalidTemplateLibrary("Template library %s not found, tried %s" % (library_name, ','.join(tried_modules)))
    9811027    return lib
    9821028
    983 def add_to_builtins(module_name):
    984     builtins.append(get_library(module_name))
     1029def add_to_builtins(module):
     1030    builtins.append(import_library(module))
    9851031
    9861032add_to_builtins('django.template.defaulttags')
    9871033add_to_builtins('django.template.defaultfilters')
  • django/template/defaulttags.py

    diff -r 6eb3c3a60842 django/template/defaulttags.py
    a b  
    920920    for taglib in bits[1:]:
    921921        # add the library to the parser
    922922        try:
    923             lib = get_library("django.templatetags.%s" % taglib)
     923            lib = get_library(taglib)
    924924            parser.add_library(lib)
    925925        except InvalidTemplateLibrary, e:
    926926            raise TemplateSyntaxError("'%s' is not a valid tag library: %s" %
  • django/templatetags/__init__.py

    diff -r 6eb3c3a60842 django/templatetags/__init__.py
    a b  
    1 from django.conf import settings
    2 from django.utils import importlib
    3 
    4 for a in settings.INSTALLED_APPS:
    5     try:
    6         __path__.extend(importlib.import_module('.templatetags', a).__path__)
    7     except ImportError:
    8         pass
  • tests/regressiontests/templates/tests.py

    diff -r 6eb3c3a60842 tests/regressiontests/templates/tests.py
    a b  
    5959
    6060register.tag("echo", do_echo)
    6161
    62 template.libraries['django.templatetags.testtags'] = register
     62template.libraries['testtags'] = register
    6363
    6464#####################################
    6565# Helper objects for template tests #
Back to Top