Ticket #2539: template_04.diff
File template_04.diff, 5.5 KB (added by , 17 years ago) |
---|
-
django/templatetags/__init__.py
2 2 3 3 for a in settings.INSTALLED_APPS: 4 4 try: 5 path = __import__(a[:a.rindex('.')], '', '', ['']).__path__ 6 for i in path: 7 if not i in __path__: 8 __path__.append(i) 5 9 __path__.extend(__import__(a + '.templatetags', {}, {}, ['']).__path__) 6 10 except ImportError: 7 11 pass -
django/template/__init__.py
63 63 from django.utils.text import smart_split 64 64 from django.utils.encoding import smart_unicode, force_unicode 65 65 from django.utils.translation import ugettext as _ 66 from django.utils.datastructures import SortedDict 66 67 67 68 __all__ = ('Template', 'Context', 'RequestContext', 'compile_string') 68 69 … … 261 262 self.tokens = tokens 262 263 self.tags = {} 263 264 self.filters = {} 265 self.libs = {} 264 266 for lib in builtins: 265 267 self.add_library(lib) 266 268 … … 289 291 # execute callback function for this tag and append resulting node 290 292 self.enter_command(command, token) 291 293 try: 292 compile_func = self. tags[command]294 compile_func = self.get_tags(command) 293 295 except KeyError: 294 296 self.invalid_block_tag(token, command) 295 297 try: … … 355 357 def add_library(self, lib): 356 358 self.tags.update(lib.tags) 357 359 self.filters.update(lib.filters) 360 #save index of lib 361 if hasattr(lib, 'appname'): 362 d = self.libs.setdefault(lib.taglibname, SortedDict({})) 363 d[lib.appname] = {'tags':lib.tags, 'filters': lib.filters} 358 364 365 def get_tags(self, tag): 366 v = tag.split('.') 367 if len(v) == 3: 368 appname, taglibname, command = v 369 return self.libs[taglibname][appname]['tags'][command] 370 elif len(v) == 2: 371 taglibname, command = v 372 return self.libs[taglibname].values()[0]['tags'][command] 373 else: 374 return self.tags[tag] 375 376 def get_filters(self, filter_name): 377 v = filter_name.split('.') 378 if len(v) == 3: 379 appname, taglibname, command = v 380 return self.libs[taglibname][appname]['filters'][command] 381 elif len(v) == 2: 382 taglibname, command = v 383 return self.libs[taglibname].values()[0]['filters'][command] 384 else: 385 return self.filters[filter_name] 386 359 387 def compile_filter(self, token): 360 388 "Convenient wrapper for FilterExpression" 361 389 return FilterExpression(token, self) 362 390 363 391 def find_filter(self, filter_name): 364 if filter_name in self.filters: 365 return self.filters[filter_name] 366 else: 392 try: 393 return self.get_filters(filter_name) 394 except: 395 import traceback 396 traceback.print_exc() 367 397 raise TemplateSyntaxError, "Invalid filter: '%s'" % filter_name 368 398 369 399 class DebugParser(Parser): … … 503 533 ^"(?P<constant>%(str)s)"| 504 534 ^(?P<var>[%(var_chars)s]+)| 505 535 (?:%(filter_sep)s 506 (?P<filter_name> \w+)536 (?P<filter_name>[\w.]+) 507 537 (?:%(arg_sep)s 508 538 (?: 509 539 %(i18n_open)s"(?P<i18n_arg>%(str)s)"%(i18n_close)s| … … 914 944 return func 915 945 return dec 916 946 947 import os.path 917 948 def get_library(module_name): 918 949 lib = libraries.get(module_name, None) 919 950 if not lib: 920 951 try: 921 952 mod = __import__(module_name, {}, {}, ['']) 953 #get the appname and tag module name from the __file__ 954 v = mod.__file__.replace('\\', '/').split('/') 955 if v[-2] == 'templatetags': 956 appname, taglibname = v[-3], os.path.splitext(v[-1])[0] 957 else: 958 appname, taglibname = None, None 922 959 except ImportError, e: 960 import traceback 961 traceback.print_exc() 923 962 raise InvalidTemplateLibrary, "Could not load template library from %s, %s" % (module_name, e) 924 963 try: 925 964 lib = mod.register 965 966 # assign appname and taglibname to lib 967 lib.appname = appname 968 lib.taglibname = taglibname 926 969 libraries[module_name] = lib 927 970 except AttributeError: 928 971 raise InvalidTemplateLibrary, "Template library %s does not have a variable named 'register'" % module_name -
django/template/defaulttags.py
795 795 for taglib in bits[1:]: 796 796 # add the library to the parser 797 797 try: 798 lib = get_library("django.templatetags.%s" % taglib.split('.')[-1]) 798 if taglib.find('.') > -1: 799 appname, module = taglib.split('.') 800 taglib = '.'.join([appname, 'templatetags', module]) 801 lib = get_library("django.templatetags.%s" % taglib) 799 802 parser.add_library(lib) 800 803 except InvalidTemplateLibrary, e: 804 import traceback 805 traceback.print_exc() 801 806 raise TemplateSyntaxError, "'%s' is not a valid tag library: %s" % (taglib, e) 802 807 return LoadNode() 803 808 load = register.tag(load)