Ticket #208: 208.patch
File 208.patch, 7.6 KB (added by , 18 years ago) |
---|
-
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 … … 372 373 the loop:: 373 374 374 375 {% for o in some_list %} 375 <tr class="{% cycle row1,row2%}">376 <tr class="{% cycle 'row1' 'row2' %}"> 376 377 ... 377 378 </tr> 378 379 {% endfor %} … … 380 381 Outside of a loop, give the values a unique name the first time you call 381 382 it, then use that name each sucessive time through:: 382 383 383 <tr class="{% cycle row1,row2,row3as rowcolors %}">...</tr>384 <tr class="{% cycle 'row1' 'row2' 'row3' as rowcolors %}">...</tr> 384 385 <tr class="{% cycle rowcolors %}">...</tr> 385 386 <tr class="{% cycle rowcolors %}">...</tr> 386 387 387 You can use any number of values, seperated by commas. Make sure not to 388 put spaces between the values -- only commas. 388 You can use any number of values, seperated by spaces. 389 389 """ 390 390 391 391 # Note: This returns the exact same node on each {% cycle name %} call; that 392 # is, the node object returned from {% cycle a ,b,c as name %} and the one392 # is, the node object returned from {% cycle a b c as name %} and the one 393 393 # returned from {% cycle name %} are the exact same object. This shouldn't 394 394 # cause problems (heh), but if it does, now you know. 395 395 # … … 398 398 # a global variable, which would make cycle names have to be unique across 399 399 # *all* templates. 400 400 401 args = token.contents.split() 401 args = token.split_contents() 402 402 403 if len(args) < 2: 403 raise TemplateSyntaxError("'Cycle' statement requires at least two arguments")404 raise TemplateSyntaxError("Cycle' statement requires at least two arguments") 404 405 405 elif len(args) == 2 and "," in args[1]: 406 # {% cycle a,b,c %} 407 cyclevars = [v for v in args[1].split(",") if v] # split and kill blanks 408 return CycleNode(cyclevars) 409 # {% cycle name %} 410 411 elif len(args) == 2: 406 if len(args) == 2: 407 # {% cycle foo %} case 412 408 name = args[1] 413 409 if not hasattr(parser, '_namedCycleNodes'): 414 410 raise TemplateSyntaxError("No named cycles in template: '%s' is not defined" % name) … … 416 412 raise TemplateSyntaxError("Named cycle '%s' does not exist" % name) 417 413 return parser._namedCycleNodes[name] 418 414 419 elif len(args) == 4: 420 # {% cycle a,b,c as name %} 421 if args[2] != 'as': 422 raise TemplateSyntaxError("Second 'cycle' argument must be 'as'") 423 cyclevars = [v for v in args[1].split(",") if v] # split and kill blanks 424 name = args[3] 425 node = CycleNode(cyclevars, name) 426 427 if not hasattr(parser, '_namedCycleNodes'): 428 parser._namedCycleNodes = {} 429 430 parser._namedCycleNodes[name] = node 415 if len(args) > 2 and args[-2]: 416 if args[-2] == 'as': 417 # {% cycle 'value 1' 'value 2' 'value 3' as foo %} case 418 name = args[-1] 419 cyclevars = args[1:-2] 420 node = CycleNode(cyclevars, name) 421 if not hasattr(parser, '_namedCycleNodes'): 422 parser._namedCycleNodes = {} 423 parser._namedCycleNodes[name] = node 424 else: 425 # {% cycle 'value 1' 'value 2' 'value 3' %} case 426 cyclevars = args[1:] 427 node = CycleNode(cyclevars) 428 431 429 return node 432 430 433 else: 434 raise TemplateSyntaxError("Invalid arguments to 'cycle': %s" % args) 431 # none of the cases were met 432 raise TemplateSyntaxError("Invalid arguments to 'cycle': %s" % args) 433 435 434 cycle = register.tag(cycle) 436 435 437 436 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'), 243 247 244 248 ### EXCEPTIONS ############################################################ 245 249 -
docs/templates.txt
364 364 Within a loop, cycles among the given strings each time through the loop:: 365 365 366 366 {% for o in some_list %} 367 <tr class="{% cycle row1,row2%}">367 <tr class="{% cycle 'row1' 'row2' %}"> 368 368 ... 369 369 </tr> 370 370 {% endfor %} … … 372 372 Outside of a loop, give the values a unique name the first time you call it, 373 373 then use that name each successive time through:: 374 374 375 <tr class="{% cycle row1,row2,row3as rowcolors %}">...</tr>375 <tr class="{% cycle 'row1' 'row2' 'row3' as rowcolors %}">...</tr> 376 376 <tr class="{% cycle rowcolors %}">...</tr> 377 377 <tr class="{% cycle rowcolors %}">...</tr> 378 378 379 You can use any number of values, separated by commas. Make sure not to put 380 spaces between the values -- only commas. 379 You can use any number of values, separated by spaces. 381 380 382 381 debug 383 382 ~~~~~