@register.tag(u'for')
def do_for(parser, token):
 bits = token.split_contents()
 if (len(bits) < 4):
  raise TemplateSyntaxError((u"'for' statements should have at least four words: %s" % token.contents))
 is_reversed = (bits[(-1)] == u'reversed')
 in_index = ((-3) if is_reversed else (-2))
 if (bits[in_index] != u'in'):
  raise TemplateSyntaxError((u"'for' statements should use the format 'for x in y': %s" % token.contents))
 loopvars = re.split(u' *, *', u' '.join(bits[1:in_index]))
 for var in loopvars:
  if ((not var) or (u' ' in var)):
   raise TemplateSyntaxError((u"'for' tag received an invalid argument: %s" % token.contents))
 sequence = parser.compile_filter(bits[(in_index + 1)])
 nodelist_loop = parser.parse((u'empty', u'endfor'))
 token = parser.next_token()
 if (token.contents == u'empty'):
  nodelist_empty = parser.parse((u'endfor',))
  parser.delete_first_token()
 else:
  nodelist_empty = None
 return ForNode(loopvars, sequence, is_reversed, nodelist_loop, nodelist_empty)

@ register . tag ( u'for' ) def do_for ( parser , token ) : bits = token . split_contents ( ) if ( len ( bits ) < 4 ) : raise TemplateSyntaxError ( ( u"'for' statements should have at least four words: %s" % token . contents ) ) is_reversed = ( bits [ ( - 1 ) ] == u'reversed' ) in_index = ( ( - 3 ) if is_reversed else ( - 2 ) ) if ( bits [ in_index ] != u'in' ) : raise TemplateSyntaxError ( ( u"'for' statements should use the format 'for x in y': %s" % token . contents ) ) loopvars = re . split ( u' *, *' , u' ' . join ( bits [ 1 : in_index ] ) ) for var in loopvars : if ( ( not var ) or ( u' ' in var ) ) : raise TemplateSyntaxError ( ( u"'for' tag received an invalid argument: %s" % token . contents ) ) sequence = parser . compile_filter ( bits [ ( in_index + 1 ) ] ) nodelist_loop = parser . parse ( ( u'empty' , u'endfor' ) ) token = parser . next_token ( ) if ( token . contents == u'empty' ) : nodelist_empty = parser . parse ( ( u'endfor' , ) ) parser . delete_first_token ( ) else : nodelist_empty = None return ForNode ( loopvars , sequence , is_reversed , nodelist_loop , nodelist_empty )
loop over each item in an array .