Ticket #6587: new_template_lib_loader.diff

File new_template_lib_loader.diff, 2.8 KB (added by oyvind, 7 years ago)

new template library loader

  • django/template/__init__.py

    diff --git a/django/template/__init__.py b/django/template/__init__.py
    index 29daa76..9b24cba 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
    62 
     62from django.templatetags import get_library_names
    6363__all__ = ('Template', 'Context', 'RequestContext', 'compile_string')
    6464
    6565TOKEN_TEXT = 0
    class Library(object): 
    910910            return func
    911911        return dec
    912912
    913 def get_library(module_name):
     913def get_library(module_name, full_path=False):
    914914    lib = libraries.get(module_name, None)
    915915    if not lib:
     916        library_names = get_library_names()
    916917        try:
    917             mod = __import__(module_name, {}, {}, [''])
     918            library_name = full_path and module_name or library_names[module_name]
     919            mod = __import__(library_name, {}, {}, [''])
    918920        except ImportError, e:
    919921            raise InvalidTemplateLibrary("Could not load template library from %s, %s" % (module_name, e))
    920922        try:
    def get_library(module_name): 
    925927    return lib
    926928
    927929def add_to_builtins(module_name):
    928     builtins.append(get_library(module_name))
     930    builtins.append(get_library(module_name, full_path=True))
    929931
    930932add_to_builtins('django.template.defaulttags')
    931933add_to_builtins('django.template.defaultfilters')
  • django/templatetags/__init__.py

    diff --git a/django/templatetags/__init__.py b/django/templatetags/__init__.py
    index 9204535..a4c2931 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
     4library_names = {}
     5
     6def get_library_names():
     7    if not library_names:
     8        for a in settings.INSTALLED_APPS:
     9            try:
     10                appname = a.find('.')!=-1 and a.split('.')[-1] or a
     11                name = a + '.templatetags'
     12                mod = __import__(name, {}, {}, [''])
     13                for root, dirs, files in os.walk(mod.__path__[0]):
     14                    for file in files:
     15                        if not file.endswith('.pyc') and not file.startswith('__init__'):
     16                            library_name = os.path.splitext(file)[0]
     17                            library_names[library_name] = name + '.' + library_name
     18                            library_names[appname + '.' + library_name] = name + '.' + library_name
     19            except ImportError:
     20                pass
     21        library_names['i18n'] = 'django.templatetags.i18n'
     22        library_names['cache'] = 'django.templatetags.cache'
     23    return library_names
Back to Top