Various cleanups and added custom cycler.

--HG--
branch : trunk
diff --git a/jinja2/utils.py b/jinja2/utils.py
index f0ae6a9..338db4a 100644
--- a/jinja2/utils.py
+++ b/jinja2/utils.py
@@ -10,6 +10,7 @@
 """
 import re
 import sys
+import errno
 try:
     from thread import allocate_lock
 except ImportError:
@@ -173,6 +174,17 @@
             raise
 
 
+def open_if_exists(filename, mode='r'):
+    """Returns a file descriptor for the filename if that file exists,
+    otherwise `None`.
+    """
+    try:
+        return file(filename, mode)
+    except IOError, e:
+        if e.errno not in (errno.ENOENT, errno.EISDIR):
+            raise
+
+
 def pformat(obj, verbose=False):
     """Prettyprint an object.  Either use the `pretty` library or the
     builtin `pprint`.
@@ -648,6 +660,31 @@
     pass
 
 
+class Cycler(object):
+    """A cycle helper for templates."""
+
+    def __init__(self, *items):
+        if not items:
+            raise RuntimeError('at least one item has to be provided')
+        self.items = items
+        self.reset()
+
+    def reset(self):
+        """Resets the cycle."""
+        self.pos = 0
+
+    @property
+    def current(self):
+        """Returns the current item."""
+        return self.items[self.pos]
+
+    def next(self):
+        """Goes one item ahead and returns it."""
+        rv = self.current
+        self.pos = (self.pos + 1) % len(self.items)
+        return rv
+
+
 # we have to import it down here as the speedups module imports the
 # markup type which is define above.
 try: