Ticket #5908: resetcycle.diff
File resetcycle.diff, 2.4 KB (added by , 17 years ago) |
---|
-
django/template/defaulttags.py
41 41 def __init__(self, cyclevars, variable_name=None): 42 42 self.cycle_iter = itertools_cycle(cyclevars) 43 43 self.variable_name = variable_name 44 self.cyclevars = cyclevars 44 45 45 def render(self, context): 46 def render(self, context): 47 if context.has_key('resetcycle'): 48 if context['resetcycle'] != None: 49 reset_name = context['resetcycle'][0] 50 else: 51 reset_name = None 52 53 if self.variable_name == reset_name: 54 self.cycle_iter = itertools_cycle(self.cyclevars) 55 for d in context.dicts: 56 if 'resetcycle' in d: 57 del(d['resetcycle']) 46 58 value = self.cycle_iter.next() 47 59 value = Variable(value).resolve(context) 48 60 if self.variable_name: 49 61 context[self.variable_name] = value 50 62 return value 51 63 64 class ResetCycleNode(Node): 65 def __init__(self, variable_name=None): 66 self.variable_name = variable_name 67 68 def render(self, context): 69 context['resetcycle'] = self.variable_name 70 return '' 71 52 72 class DebugNode(Node): 53 73 def render(self, context): 54 74 from pprint import pformat … … 498 518 return node 499 519 cycle = register.tag(cycle) 500 520 521 def resetcycle(parser, token): 522 """ 523 Resets cycle tag. 524 525 If argument is given, resets the cycle named argument, else resets last 526 non-named argument. 527 """ 528 args = token.split_contents() 529 530 if len(args) > 2: 531 raise TemplateSyntaxError("'resetcycle' tag accepts maximum of one argument.") 532 533 if len(args) == 2: 534 # {% resetcycle foo %} case. 535 name = args[1] 536 if not hasattr(parser, '_namedCycleNodes'): 537 raise TemplateSyntaxError("No named cycles in template." 538 " '%s' is not defined" % name) 539 if not name in parser._namedCycleNodes: 540 raise TemplateSyntaxError("Named cycle '%s' does not exist" % name) 541 return ResetCycleNode(args[1:]) 542 return ResetCycleNode() 543 resetcycle = register.tag(resetcycle) 544 501 545 def debug(parser, token): 502 546 """ 503 547 Outputs a whole load of debugging information, including the current