#NEWLINE# def test_cycle( self ) : #NEWLINE# #INDENT# "\n        This test re-implements django's cycle tag (because it's quite crazy)\n        and checks if it works.\n        " #NEWLINE# from itertools import cycle as itertools_cycle #NEWLINE# class Cycle ( core . Tag ) : #NEWLINE# #INDENT# name = 'classy_cycle' #NEWLINE# options = core . Options ( arguments . MultiValueArgument ( 'values' ) , 'as' , arguments . Argument ( 'varname' , required = False , resolve = False ) ) #NEWLINE# def render_tag( self , context , values , varname ) : #NEWLINE# #INDENT# if ( self not in context . render_context ) : #NEWLINE# #INDENT# context . render_context [ self ] = itertools_cycle ( values ) #NEWLINE# #UNINDENT# cycle_iter = context . render_context [ self ] #NEWLINE# value = compat_next ( cycle_iter ) #NEWLINE# if varname : #NEWLINE# #INDENT# context [ varname ] = value #NEWLINE# #UNINDENT# return value #NEWLINE# #UNINDENT# #UNINDENT# origtpl = template . Template ( '{% for thing in sequence %}{% cycle "1" "2" "3" "4" %}{% endfor %}' ) #NEWLINE# sequence = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] #NEWLINE# context = template . Context ( { #NEWLINE# #INDENT# 'sequence' : sequence , #NEWLINE# #UNINDENT# } ) #NEWLINE# original = origtpl . render ( context ) #NEWLINE# with TemplateTags ( Cycle ) : #NEWLINE# #INDENT# classytpl = template . Template ( '{% for thing in sequence %}{% classy_cycle "1" "2" "3" "4" %}{% endfor %}' ) #NEWLINE# classy = classytpl . render ( context ) #NEWLINE# #UNINDENT# self . assertEqual ( original , classy ) #NEWLINE# origtpl = template . Template ( '{% for thing in sequence %}{% cycle "1" "2" "3" "4" as myvarname %}{% endfor %}' ) #NEWLINE# sequence = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] #NEWLINE# context = template . Context ( { #NEWLINE# #INDENT# 'sequence' : sequence , #NEWLINE# #UNINDENT# } ) #NEWLINE# original = origtpl . render ( context ) #NEWLINE# with TemplateTags ( Cycle ) : #NEWLINE# #INDENT# classytpl = template . Template ( '{% for thing in sequence %}{% classy_cycle "1" "2" "3" "4" as myvarname %}{% endfor %}' ) #NEWLINE# classy = classytpl . render ( self ) #NEWLINE# #UNINDENT# self . assertEqual ( original , classy )