Ticket #6587: new_template_lib_loader_7.diff
File new_template_lib_loader_7.diff, 5.0 KB (added by , 17 years ago) |
---|
-
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 59 59 from django.utils.translation import ugettext as _ 60 60 from django.utils.safestring import SafeData, EscapeData, mark_safe, mark_for_escaping 61 61 from django.utils.html import escape 62 from django.templatetags import get_app_label_and_modules 62 63 63 64 __all__ = ('Template', 'Context', 'RequestContext', 'compile_string') 64 65 … … class Library(object): 913 914 return func 914 915 return dec 915 916 916 def get_library(module_name): 917 lib = libraries.get(module_name, None) 917 def 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 927 def get_library(library_name): 928 lib = libraries.get(library_name, None) 918 929 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 919 940 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))) 928 959 return lib 929 960 930 961 def add_to_builtins(module_name): 931 builtins.append( get_library(module_name))962 builtins.append(import_library(module_name)) 932 963 933 964 add_to_builtins('django.template.defaulttags') 934 965 add_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): 850 850 for taglib in bits[1:]: 851 851 # add the library to the parser 852 852 try: 853 lib = get_library( "django.templatetags.%s" %taglib)853 lib = get_library(taglib) 854 854 parser.add_library(lib) 855 855 except InvalidTemplateLibrary, e: 856 856 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 1 1 from django.conf import settings 2 import os 2 3 3 for a in settings.INSTALLED_APPS: 4 try: 5 __path__.extend(__import__(a + '.templatetags', {}, {}, ['']).__path__) 6 except ImportError: 7 pass 4 app_labels_and_modules= [] 5 6 def 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): 45 45 46 46 register.tag("echo", do_echo) 47 47 48 template.libraries[' django.templatetags.testtags'] = register48 template.libraries['testtags'] = register 49 49 50 50 ##################################### 51 51 # Helper objects for template tests #