Ticket #2539: template_02.diff
File template_02.diff, 5.7 KB (added by , 18 years ago) |
---|
-
template/__init__.py
60 60 from django.template.context import Context, RequestContext, ContextPopException 61 61 from django.utils.functional import curry 62 62 from django.utils.text import smart_split 63 from django.utils.datastructures import SortedDict 63 64 64 65 __all__ = ('Template', 'Context', 'RequestContext', 'compile_string') 65 66 … … 223 224 self.tokens = tokens 224 225 self.tags = {} 225 226 self.filters = {} 227 self.libs = {} 226 228 for lib in builtins: 227 229 self.add_library(lib) 228 230 … … 251 253 # execute callback function for this tag and append resulting node 252 254 self.enter_command(command, token) 253 255 try: 254 compile_func = self. tags[command]256 compile_func = self.get_tags(command) 255 257 except KeyError: 256 258 self.invalid_block_tag(token, command) 257 259 try: … … 317 319 def add_library(self, lib): 318 320 self.tags.update(lib.tags) 319 321 self.filters.update(lib.filters) 322 #save index of lib 323 if lib.appname: 324 d = self.libs.setdefault(lib.taglibname, SortedDict({})) 325 d[lib.appname] = {'tags':lib.tags, 'filters':lib.filters} 320 326 327 def get_tags(self, tag): 328 329 v = tag.split('.') 330 if len(v) == 3: 331 appname, taglibname, command = v 332 return self.libs[taglibname][appname]['tags'][command] 333 elif len(v) == 2: 334 taglibname, command = v 335 return self.libs[taglibname].values()[0]['tags'][command] 336 else: 337 return self.tags[tag] 338 339 def get_filters(self, filter): 340 341 v = filter.split('.') 342 if len(v) == 3: 343 appname, taglibname, command = v 344 return self.libs[taglibname][appname]['filters'][command] 345 elif len(v) == 2: 346 taglibname, command = v 347 return self.libs[taglibname].values()[0]['filters'][command] 348 else: 349 return self.filters[filter] 350 321 351 def compile_filter(self, token): 322 352 "Convenient wrapper for FilterExpression" 323 353 return FilterExpression(token, self) 324 354 325 355 def find_filter(self, filter_name): 326 if self.filters.has_key(filter_name): 327 return self.filters[filter_name] 328 else: 356 try: 357 return self.get_filters(filter_name) 358 except: 359 import traceback 360 traceback.print_exc() 329 361 raise TemplateSyntaxError, "Invalid filter: '%s'" % filter_name 362 # if self.filters.has_key(filter_name): 363 # return self.filters[filter_name] 364 # else: 365 # raise TemplateSyntaxError, "Invalid filter: '%s'" % filter_name 330 366 331 367 class DebugParser(Parser): 332 368 def __init__(self, lexer): … … 468 504 ^"(?P<constant>%(str)s)"| 469 505 ^(?P<var>[%(var_chars)s]+)| 470 506 (?:%(filter_sep)s 471 (?P<filter_name> \w+)507 (?P<filter_name>[\w.]+) 472 508 (?:%(arg_sep)s 473 509 (?: 474 510 %(i18n_open)s"(?P<i18n_arg>%(str)s)"%(i18n_close)s| … … 873 909 return func 874 910 return dec 875 911 912 import os.path 876 913 def get_library(module_name): 877 914 lib = libraries.get(module_name, None) 878 915 if not lib: 879 916 try: 880 917 mod = __import__(module_name, '', '', ['']) 918 919 #get the appname and tag module name from the __file__ 920 v = mod.__file__.replace('\\', '/').split('/') 921 if v[-2] == 'templatetags': 922 appname, taglibname = v[-3], os.path.splitext(v[-1])[0] 923 else: 924 appname, taglibname = None, None 881 925 except ImportError, e: 926 import traceback 927 traceback.print_exc() 882 928 raise InvalidTemplateLibrary, "Could not load template library from %s, %s" % (module_name, e) 883 929 try: 884 930 lib = mod.register 931 932 #assign appname and taglibname to lib 933 lib.appname = appname 934 lib.taglibname = taglibname 885 935 libraries[module_name] = lib 886 936 except AttributeError: 887 937 raise InvalidTemplateLibrary, "Template library %s does not have a variable named 'register'" % module_name -
template/defaulttags.py
686 686 for taglib in bits[1:]: 687 687 # add the library to the parser 688 688 try: 689 lib = get_library("django.templatetags.%s" % taglib.split('.')[-1]) 689 if taglib.find('.') > -1: 690 appname, module = taglib.split('.') 691 taglib = '.'.join([appname, 'templatetags', module]) 692 lib = get_library("django.templatetags.%s" % taglib) 690 693 parser.add_library(lib) 691 694 except InvalidTemplateLibrary, e: 695 import traceback 696 traceback.print_exc() 692 697 raise TemplateSyntaxError, "'%s' is not a valid tag library: %s" % (taglib, e) 693 698 return LoadNode() 694 699 load = register.tag(load) -
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