Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Made use of `itertools.cycle` for the `cycle` template tag.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6636 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 2a48fc5007b9fc04f6483ddf022d504bd11e0826 1 parent 5997cb8
Gary Wilson Jr. authored

Showing 1 changed file with 4 additions and 5 deletions. Show diff stats Hide diff stats

  1. 9  django/template/defaulttags.py
9  django/template/defaulttags.py
... ...
@@ -1,5 +1,7 @@
1 1
 "Default tags used by the template system, available to all templates."
2 2
 
  3
+from itertools import cycle as itertools_cycle
  4
+
3 5
 from django.template import Node, NodeList, Template, Context, Variable
4 6
 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 7
 from django.template import get_library, Library, InvalidTemplateLibrary
@@ -22,14 +24,11 @@ def render(self, context):
22 24
 
23 25
 class CycleNode(Node):
24 26
     def __init__(self, cyclevars, variable_name=None):
25  
-        self.cyclevars = cyclevars
26  
-        self.cyclevars_len = len(cyclevars)
27  
-        self.counter = -1
  27
+        self.cycle_iter = itertools_cycle(cyclevars)
28 28
         self.variable_name = variable_name
29 29
 
30 30
     def render(self, context):
31  
-        self.counter += 1
32  
-        value = self.cyclevars[self.counter % self.cyclevars_len]
  31
+        value = self.cycle_iter.next()
33 32
         value = Variable(value).resolve(context)
34 33
         if self.variable_name:
35 34
             context[self.variable_name] = value

0 notes on commit 2a48fc5

Please sign in to comment.
Something went wrong with that request. Please try again.