Ticket #13567: defaulttags.patch
File defaulttags.patch, 2.3 KB (added by , 14 years ago) |
---|
-
defaulttags.py
53 53 return u'' 54 54 55 55 class CycleNode(Node): 56 def __init__(self, cyclevars, variable_name=None ):56 def __init__(self, cyclevars, variable_name=None, silent=None): 57 57 self.cyclevars = cyclevars 58 58 self.variable_name = variable_name 59 self.silent = silent 59 60 60 61 def render(self, context): 61 62 if self not in context.render_context: … … 64 65 value = cycle_iter.next().resolve(context) 65 66 if self.variable_name: 66 67 context[self.variable_name] = value 68 if self.silent is not None and self.silent.resolve(context): 69 return u'' 67 70 return value 68 71 69 72 class DebugNode(Node): … … 475 478 You can use any number of values, separated by spaces. Commas can also 476 479 be used to separate values; if a comma is used, the cycle values are 477 480 interpreted as literal strings. 481 482 You can now also pass a 'silent' flag (a var which will be evaluated as 483 boolean) to supress the return string when you are using the 'as' form, eg: 484 485 {% cycle 'row1' 'row2' 'row3' as rowcolors 0 %} (no output) 486 478 487 """ 479 488 480 489 # Note: This returns the exact same node on each {% cycle name %} call; … … 505 514 if not name in parser._namedCycleNodes: 506 515 raise TemplateSyntaxError("Named cycle '%s' does not exist" % name) 507 516 return parser._namedCycleNodes[name] 508 509 if len(args) > 4 and args[-2] == 'as': 510 name = args[-1] 511 values = [parser.compile_filter(arg) for arg in args[1:-2]] 512 node = CycleNode(values, name) 517 518 try: 519 as_index = args.index('as') 520 except ValueError: 521 as_index = None 522 523 if len(args) > 4 and as_index is not None: 524 name = args[as_index+1] 525 values = [parser.compile_filter(arg) for arg in args[1:as_index]] 526 if len(args) > as_index + 2: 527 node = CycleNode(values, name, parser.compile_filter(args[as_index+2])) 528 else: 529 node = CycleNode(values, name) 513 530 if not hasattr(parser, '_namedCycleNodes'): 514 531 parser._namedCycleNodes = {} 515 532 parser._namedCycleNodes[name] = node