Ticket #208: 208.3.patch
File 208.3.patch, 10.4 KB (added by , 18 years ago) |
---|
-
django/contrib/admin/templates/admin/change_list_results.html
10 10 </thead> 11 11 <tbody> 12 12 {% for result in results %} 13 <tr class="{% cycle row1,row2%}">{% for item in result %}{{ item }}{% endfor %}</tr>13 <tr class="{% cycle 'row1' 'row2' %}">{% for item in result %}{{ item }}{% endfor %}</tr> 14 14 {% endfor %} 15 15 </tbody> 16 16 </table> -
django/template/defaulttags.py
2 2 3 3 from django.template import Node, NodeList, Template, Context, resolve_variable 4 4 from django.template import TemplateSyntaxError, VariableDoesNotExist, BLOCK_TAG_START, BLOCK_TAG_END, VARIABLE_TAG_START, VARIABLE_TAG_END, SINGLE_BRACE_START, SINGLE_BRACE_END, COMMENT_TAG_START, COMMENT_TAG_END 5 from django.template import get_library, Library, InvalidTemplateLibrary 5 from django.template import get_library, Library, InvalidTemplateLibrary, resolve_variable 6 6 from django.conf import settings 7 7 import sys 8 8 … … 22 22 def render(self, context): 23 23 self.counter += 1 24 24 value = self.cyclevars[self.counter % self.cyclevars_len] 25 value = resolve_variable(value, context) 25 26 if self.variable_name: 26 27 context[self.variable_name] = value 27 28 return value … … 389 390 the loop:: 390 391 391 392 {% for o in some_list %} 392 <tr class="{% cycle row1,row2%}">393 <tr class="{% cycle 'row1' 'row2' %}"> 393 394 ... 394 395 </tr> 395 396 {% endfor %} … … 397 398 Outside of a loop, give the values a unique name the first time you call 398 399 it, then use that name each sucessive time through:: 399 400 400 <tr class="{% cycle row1,row2,row3as rowcolors %}">...</tr>401 <tr class="{% cycle 'row1' 'row2' 'row3' as rowcolors %}">...</tr> 401 402 <tr class="{% cycle rowcolors %}">...</tr> 402 403 <tr class="{% cycle rowcolors %}">...</tr> 403 404 404 You can use any number of values, seperated by commas. Make sure not to 405 put spaces between the values -- only commas. 405 You can use any number of values, seperated by spaces. 406 406 """ 407 407 408 408 # Note: This returns the exact same node on each {% cycle name %} call; that 409 # is, the node object returned from {% cycle a ,b,c as name %} and the one409 # is, the node object returned from {% cycle a b c as name %} and the one 410 410 # returned from {% cycle name %} are the exact same object. This shouldn't 411 411 # cause problems (heh), but if it does, now you know. 412 412 # … … 415 415 # a global variable, which would make cycle names have to be unique across 416 416 # *all* templates. 417 417 418 args = token.contents.split() 418 args = token.split_contents() 419 419 420 if len(args) < 2: 420 raise TemplateSyntaxError("'Cycle' statement requires at least two arguments")421 raise TemplateSyntaxError("Cycle' statement requires at least two arguments") 421 422 422 elif len(args) == 2 and "," in args[1]: 423 # {% cycle a,b,c %} 424 cyclevars = [v for v in args[1].split(",") if v] # split and kill blanks 425 return CycleNode(cyclevars) 426 # {% cycle name %} 423 if len(args) == 2: 424 # {% cycle foo,bar,zoo %} case ? 425 cyclevars = args[1].split(",") 426 if len(cyclevars) > 1: 427 # we need to add the extra quotation marks around the values 428 cyclevars = ['"%s"' % x for x in cyclevars] 429 return CycleNode(cyclevars) 427 430 428 elif len(args) == 2: 429 name = args[1] 430 if not hasattr(parser, '_namedCycleNodes'): 431 raise TemplateSyntaxError("No named cycles in template: '%s' is not defined" % name) 432 if not parser._namedCycleNodes.has_key(name): 433 raise TemplateSyntaxError("Named cycle '%s' does not exist" % name) 434 return parser._namedCycleNodes[name] 431 else: 432 # {% cycle foo %} case 433 name = args[1] 434 if not hasattr(parser, '_namedCycleNodes'): 435 raise TemplateSyntaxError("No named cycles in template: '%s' is not defined" % name) 436 if not parser._namedCycleNodes.has_key(name): 437 raise TemplateSyntaxError("Named cycle '%s' does not exist" % name) 438 return parser._namedCycleNodes[name] 435 439 436 elif len(args) == 4: 437 # {% cycle a,b,c as name %} 438 if args[2] != 'as': 439 raise TemplateSyntaxError("Second 'cycle' argument must be 'as'") 440 cyclevars = [v for v in args[1].split(",") if v] # split and kill blanks 440 if len(args) == 4 and args[2] == "as" and len(args[1].split(",")) > 1: 441 # {% cycle a,b,c as foo %} case 442 cyclevars = ['"%s"' % x for x in args[1].split(",")] 441 443 name = args[3] 442 444 node = CycleNode(cyclevars, name) 443 444 445 if not hasattr(parser, '_namedCycleNodes'): 445 446 parser._namedCycleNodes = {} 446 447 447 parser._namedCycleNodes[name] = node 448 448 return node 449 449 450 else: 451 raise TemplateSyntaxError("Invalid arguments to 'cycle': %s" % args) 450 if len(args) > 2 and args[-2]: 451 if args[-2] == 'as': 452 # {% cycle 'value 1' 'value 2' 'value 3' as foo %} case 453 name = args[-1] 454 cyclevars = args[1:-2] 455 print name, cyclevars 456 node = CycleNode(cyclevars, name) 457 if not hasattr(parser, '_namedCycleNodes'): 458 parser._namedCycleNodes = {} 459 parser._namedCycleNodes[name] = node 460 else: 461 # {% cycle 'value 1' 'value 2' 'value 3' %} case 462 cyclevars = args[1:] 463 node = CycleNode(cyclevars) 464 465 return node 466 467 # none of the cases were met 468 raise TemplateSyntaxError("Invalid arguments to 'cycle': %s" % args) 469 452 470 cycle = register.tag(cycle) 453 471 454 472 def debug(parser, token): -
tests/regressiontests/templates/tests.py
233 233 234 234 ### CYCLE TAG ############################################################# 235 235 'cycle01': ('{% cycle a %}', {}, template.TemplateSyntaxError), 236 'cycle02': ('{% cycle a,b,c as abc %}{% cycle abc %}', {}, 'ab'), 237 'cycle03': ('{% cycle a,b,c as abc %}{% cycle abc %}{% cycle abc %}', {}, 'abc'), 238 'cycle04': ('{% cycle a,b,c as abc %}{% cycle abc %}{% cycle abc %}{% cycle abc %}', {}, 'abca'), 239 'cycle05': ('{% cycle %}', {}, template.TemplateSyntaxError), 240 'cycle06': ('{% cycle a %}', {}, template.TemplateSyntaxError), 241 'cycle07': ('{% cycle a,b,c as foo %}{% cycle bar %}', {}, template.TemplateSyntaxError), 242 'cycle08': ('{% cycle a,b,c as foo %}{% cycle foo %}{{ foo }}{{ foo }}{% cycle foo %}{{ foo }}', {}, 'abbbcc'), 236 'cycle02': ("{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}", {}, 'ab'), 237 'cycle03': ("{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}{% cycle abc %}", {}, 'abc'), 238 'cycle04': ("{% cycle 'a' 'b' 'c' as abc %}{% cycle abc %}{% cycle abc %}{% cycle abc %}", {}, 'abca'), 239 'cycle05': ("{% cycle %}", {}, template.TemplateSyntaxError), 240 'cycle06': ("{% cycle a %}", {}, template.TemplateSyntaxError), 241 'cycle07': ("{% cycle 'a' 'b' 'c' as foo %}{% cycle bar %}", {}, template.TemplateSyntaxError), 242 'cycle08': ("{% cycle 'a' 'b' 'c' as foo %}{% cycle foo %}{{ foo }}{{ foo }}{% cycle foo %}{{ foo }}", 243 {}, 'abbbcc'), 244 'cycle09': ("{% cycle 'chris 1' 'camri is cute' 'cotton' as foo %}{% cycle foo %}", {}, 'chris 1camri is cute'), 245 'cycle10': ("{% for i in test %}{% cycle 'row1' 'row2' %}{% endfor %}", {'test':[1,2]}, 'row1row2'), 246 'cycle11': ("{% cycle one two as foo %}{% cycle foo %}", {'one':'1','two':'2'}, '12'), 247 'cycle12': ('{% cycle a,b,c as abc %}{% cycle abc %}', {}, 'ab'), 248 'cycle13': ('{% cycle a,b,c as abc %}{% cycle abc %}{% cycle abc %}', {}, 'abc'), 249 'cycle14': ('{% cycle a,b,c as abc %}{% cycle abc %}{% cycle abc %}{% cycle abc %}', {}, 'abca'), 250 'cycle15': ('{% cycle %}', {}, template.TemplateSyntaxError), 251 'cycle16': ('{% cycle a %}', {}, template.TemplateSyntaxError), 252 'cycle17': ('{% cycle a,b,c as foo %}{% cycle bar %}', {}, template.TemplateSyntaxError), 253 'cycle18': ('{% cycle a,b,c as foo %}{% cycle foo %}{{ foo }}{{ foo }}{% cycle foo %}{{ foo }}', {}, 'abbbcc'), 243 254 244 255 ### EXCEPTIONS ############################################################ 245 256 -
docs/templates.txt
362 362 Within a loop, cycles among the given strings each time through the loop:: 363 363 364 364 {% for o in some_list %} 365 <tr class="{% cycle 'row1' 'row2' %}"> 366 ... 367 </tr> 368 {% endfor %} 369 370 Or like this:: 371 372 {% for o in some_list %} 365 373 <tr class="{% cycle row1,row2 %}"> 366 374 ... 367 375 </tr> 368 376 {% endfor %} 369 377 378 Both codes does the same job, but when you are using the comma seperated list you 379 can not use blank spaces in the values. 380 370 381 Outside of a loop, give the values a unique name the first time you call it, 371 382 then use that name each successive time through:: 372 383 384 <tr class="{% cycle 'row1' 'row2' 'row3' as rowcolors %}">...</tr> 385 <tr class="{% cycle rowcolors %}">...</tr> 386 <tr class="{% cycle rowcolors %}">...</tr> 387 388 You can use any number of values, separated by spaces. 389 390 The example over can also be written like this:: 391 373 392 <tr class="{% cycle row1,row2,row3 as rowcolors %}">...</tr> 374 393 <tr class="{% cycle rowcolors %}">...</tr> 375 394 <tr class="{% cycle rowcolors %}">...</tr> 376 395 377 You can use any number of values, separated by commas. Make sure not to put 378 spaces between the values -- only commas. 396 Remember that when using the comma seperated list you can not use blank spaces. 379 397 380 398 debug 381 399 ~~~~~