Ticket #1199: ticket-1199.patch
File ticket-1199.patch, 3.9 KB (added by , 17 years ago) |
---|
-
django/template/__init__.py
438 438 (?:%(filter_sep)s 439 439 (?P<filter_name>\w+) 440 440 (?:%(arg_sep)s 441 (?P<args> 441 442 (?: 442 %(i18n_open)s" (?P<i18n_arg>%(str)s)"%(i18n_close)s|443 " (?P<constant_arg>%(str)s)"|444 (?P<var_arg>[%(var_chars)s]+)443 %(i18n_open)s"%(str)s"%(i18n_close)s| 444 "%(str)s"| 445 [%(var_chars)s]+ 445 446 ) 447 (?: 448 ,%(i18n_open)s"%(str)s"%(i18n_close)s| 449 ,"%(str)s"| 450 ,[%(var_chars)s]+ 451 )* 452 ) 446 453 )? 447 454 )""" % { 448 455 'str': r"""[^"\\]*(?:\\.[^"\\]*)*""", … … 456 463 filter_raw_string = filter_raw_string.replace("\n", "").replace(" ", "") 457 464 filter_re = re.compile(filter_raw_string, re.UNICODE) 458 465 466 arg_raw_string = r""" 467 %(i18n_open)s"(?P<i18n_arg>%(str)s)"%(i18n_close)s| 468 "(?P<constant_arg>%(str)s)"| 469 (?P<var_arg>[%(var_chars)s]+)""" % { 470 'str': r"""[^"\\]*(?:\\.[^"\\]*)*""", 471 'var_chars': "\w\." , 472 'i18n_open' : re.escape("_("), 473 'i18n_close' : re.escape(")"), 474 } 475 476 arg_raw_string = arg_raw_string.replace("\n", "").replace(" ", "") 477 arg_re = re.compile(arg_raw_string, re.UNICODE) 478 459 479 class FilterExpression(object): 460 480 """ 461 481 Parses a variable token and its optional filters (all as a single string), … … 497 517 else: 498 518 filter_name = match.group("filter_name") 499 519 args = [] 500 constant_arg, i18n_arg, var_arg = match.group("constant_arg", "i18n_arg", "var_arg") 501 if i18n_arg: 502 args.append((False, _(i18n_arg.replace(r'\"', '"')))) 503 elif constant_arg is not None: 504 args.append((False, constant_arg.replace(r'\"', '"'))) 505 elif var_arg: 506 args.append((True, Variable(var_arg))) 520 arg_group = match.group("args") 521 if arg_group: 522 for arg_match in arg_re.finditer(arg_group): 523 constant_arg, i18n_arg, var_arg = arg_match.group("constant_arg", "i18n_arg", "var_arg") 524 if i18n_arg: 525 args.append((False, _(i18n_arg.replace(r'\"', '"')))) 526 elif constant_arg is not None: 527 args.append((False, constant_arg.replace(r'\"', '"'))) 528 elif var_arg: 529 args.append((True, Variable(var_arg))) 507 530 filter_func = parser.find_filter(filter_name) 508 531 self.args_check(filter_name,filter_func, args) 509 532 filters.append( (filter_func,args)) -
tests/regressiontests/templates/tests.py
45 45 46 46 register.tag("echo", do_echo) 47 47 48 def multi_args(inp, arg1, const1, const2, i18n): 49 return u"%s %s %s %s %s" % (inp, arg1, const1, const2, i18n) 50 51 register.filter("multi_args", multi_args) 52 48 53 template.libraries['django.templatetags.testtags'] = register 49 54 50 55 ##################################### … … 359 364 360 365 # Numbers as filter arguments should work 361 366 'filter-syntax19': ('{{ var|truncatewords:1 }}', {"var": "hello world"}, "hello ..."), 367 368 # Filters can accept multiple arguments 369 'filter-syntax20': ('{% load testtags %}{{ var1|multi_args:var2,",",12,_("bar")}}', {'var1': 99, 'var2': 'foo'}, "99 foo , 12 bar"), 362 370 363 371 ### COMMENT SYNTAX ######################################################## 364 372 'comment-syntax01': ("{# this is hidden #}hello", {}, "hello"),