Ticket #208: 208.4.patch
File 208.4.patch, 6.7 KB (added by , 17 years ago) |
---|
-
django/template/defaulttags.py
30 30 def render(self, context): 31 31 self.counter += 1 32 32 value = self.cyclevars[self.counter % self.cyclevars_len] 33 value = resolve_variable(value, context) 33 34 if self.variable_name: 34 35 context[self.variable_name] = value 35 36 return value … … 403 404 the loop:: 404 405 405 406 {% for o in some_list %} 406 <tr class="{% cycle row1,row2%}">407 <tr class="{% cycle 'row1' 'row2' %}"> 407 408 ... 408 409 </tr> 409 410 {% endfor %} … … 411 412 Outside of a loop, give the values a unique name the first time you call 412 413 it, then use that name each sucessive time through:: 413 414 414 <tr class="{% cycle row1,row2,row3as rowcolors %}">...</tr>415 <tr class="{% cycle 'row1' 'row2' 'row3' as rowcolors %}">...</tr> 415 416 <tr class="{% cycle rowcolors %}">...</tr> 416 417 <tr class="{% cycle rowcolors %}">...</tr> 417 418 418 You can use any number of values, seperated by commas. Make sure not to 419 put spaces between the values -- only commas. 419 You can use any number of values, seperated by spaces. 420 420 """ 421 421 422 422 # Note: This returns the exact same node on each {% cycle name %} call; that 423 # is, the node object returned from {% cycle a ,b,c as name %} and the one423 # is, the node object returned from {% cycle a b c as name %} and the one 424 424 # returned from {% cycle name %} are the exact same object. This shouldn't 425 425 # cause problems (heh), but if it does, now you know. 426 426 # … … 429 429 # a global variable, which would make cycle names have to be unique across 430 430 # *all* templates. 431 431 432 args = token.contents.split() 432 args = token.split_contents() 433 433 434 if len(args) < 2: 434 raise TemplateSyntaxError("'Cycle' statementrequires at least two arguments")435 raise TemplateSyntaxError("'cycle' tag requires at least two arguments") 435 436 436 elif len(args) == 2 and "," in args[1]: 437 # {% cycle a,b,c %} 438 cyclevars = [v for v in args[1].split(",") if v] # split and kill blanks 439 return CycleNode(cyclevars) 440 # {% cycle name %} 437 if ',' in args[1]: 438 # Backwards compatibility: {% cycle a,b %} or {% cycle a,b as foo %} 439 # case. 440 args[1:2] = ['"%s"' % arg for arg in args[1].split(",")] 441 441 442 elif len(args) == 2: 442 if len(args) == 2: 443 # {% cycle foo %} case 443 444 name = args[1] 444 445 if not hasattr(parser, '_namedCycleNodes'): 445 446 raise TemplateSyntaxError("No named cycles in template: '%s' is not defined" % name) 446 if n ame notin parser._namedCycleNodes:447 if not name in parser._namedCycleNodes: 447 448 raise TemplateSyntaxError("Named cycle '%s' does not exist" % name) 448 449 return parser._namedCycleNodes[name] 449 450 450 elif len(args) == 4: 451 # {% cycle a,b,c as name %} 452 if args[2] != 'as': 453 raise TemplateSyntaxError("Second 'cycle' argument must be 'as'") 454 cyclevars = [v for v in args[1].split(",") if v] # split and kill blanks 455 name = args[3] 456 node = CycleNode(cyclevars, name) 457 451 if len(args) > 4 and args[-2] == 'as': 452 name = args[-1] 453 node = CycleNode(args[1:-2], name) 458 454 if not hasattr(parser, '_namedCycleNodes'): 459 455 parser._namedCycleNodes = {} 460 461 456 parser._namedCycleNodes[name] = node 462 return node463 464 457 else: 465 raise TemplateSyntaxError("Invalid arguments to 'cycle': %s" % args) 458 node = CycleNode(args[1:]) 459 return node 466 460 cycle = register.tag(cycle) 467 461 468 462 def debug(parser, token): -
docs/templates.txt
366 366 cycle 367 367 ~~~~~ 368 368 369 Cycle among the given strings each time this tag is encountered.369 Cycle among the given strings or variables each time this tag is encountered. 370 370 371 Within a loop, cycles among the given strings each time through the loop:: 371 Within a loop, cycles among the given strings/variables each time through the 372 loop:: 372 373 373 374 {% for o in some_list %} 374 <tr class="{% cycle row1,row2%}">375 <tr class="{% cycle 'row1' 'row2' rowvar %}"> 375 376 ... 376 377 </tr> 377 378 {% endfor %} 378 379 379 380 Outside of a loop, give the values a unique name the first time you call it, 380 381 then use that name each successive time through:: 381 382 382 <tr class="{% cycle row1,row2,row3as rowcolors %}">...</tr>383 <tr class="{% cycle 'row1' 'row2' rowvar as rowcolors %}">...</tr> 383 384 <tr class="{% cycle rowcolors %}">...</tr> 384 385 <tr class="{% cycle rowcolors %}">...</tr> 385 386 386 You can use any number of values, separated by commas. Make sure not to put 387 spaces between the values -- only commas. 387 You can use any number of values, separated by spaces. Values enclosed in 388 single (') or double quotes (") are treated as string literals, while values 389 without quotes are assumed to refer to context variables. 388 390 391 The depreciated comma-separated syntax (`{% cycle row1,row2 %}`) is still 392 supported. 393 389 394 debug 390 395 ~~~~~ 391 396 -
tests/regressiontests/templates/tests.py
306 306 'cycle06': ('{% cycle a %}', {}, template.TemplateSyntaxError), 307 307 'cycle07': ('{% cycle a,b,c as foo %}{% cycle bar %}', {}, template.TemplateSyntaxError), 308 308 'cycle08': ('{% cycle a,b,c as foo %}{% cycle foo %}{{ foo }}{{ foo }}{% cycle foo %}{{ foo }}', {}, 'abbbcc'), 309 'cycle09': ("{% for i in test %}{% cycle a,b %}{{ i }},{% endfor %}", {'test': range(5)}, 'a0,b1,a2,b3,a4,'), 310 # New format: 311 'cycle10': ("{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}", {}, 'ab'), 312 'cycle11': ("{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}{% cycle abc %}", {}, 'abc'), 313 'cycle12': ("{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}{% cycle abc %}{% cycle abc %}", {}, 'abca'), 314 'cycle13': ("{% for i in test %}{% cycle 'a' 'b' %}{{ i }},{% endfor %}", {'test': range(5)}, 'a0,b1,a2,b3,a4,'), 315 'cycle14': ("{% cycle one two as foo %}{% cycle foo %}", {'one': '1','two': '2'}, '12'), 316 'cycle13': ("{% for i in test %}{% cycle aye bee %}{{ i }},{% endfor %}", {'test': range(5), 'aye': 'a', 'bee': 'b'}, 'a0,b1,a2,b3,a4,'), 309 317 310 318 ### EXCEPTIONS ############################################################ 311 319