Skip to content
Myr

Merge pull request #1085 from pallets/docs-cycler

Before adoption: this commit came in with the history the project adopted, so no landing records it.

clarify cycler.next() behavior

authored by David Lord · committed by GitHub · parents 1b3d6ed, cde8a42 · Browse files · 2 files+54 added−25 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+54 added−25 removed

  • other:+54 added−25 removed

2 files+54 added−25 removed

modified:docs/templates.rst+22 added-19 removed
1478 unmodified lines
1479 as ``dict(foo='bar')``.14801481.. class:: cycler(\*items)14821483 The cycler allows you to cycle among values similar to how `loop.cycle`1484 works. Unlike `loop.cycle`, you can use this cycler outside of1485 loops or over multiple loops.1483 Cycle through values by yielding them one at a time, then restarting1484 once the end is reached.14851487 This can be very useful if you want to show a list of folders and1488 files with the folders on top but both in the same list with alternating1489 row colors.1486 Similar to ``loop.cycle``, but can be used outside loops or across1487 multiple loops. For example, render a list of folders and files in a1488 list, alternating giving them "odd" and "even" classes.14891491 The following example shows how `cycler` can be used::1490 .. code-block:: html+jinja14911493 {% set row_class = cycler('odd', 'even') %}1492 {% set row_class = cycler("odd", "even") %}1493 <ul class="browser">1494 {% for folder in folders %}1496 <li class="folder {{ row_class.next() }}">{{ folder|e }}</li>1495 <li class="folder {{ row_class.next() }}">{{ folder }}1496 {% endfor %}1498 {% for filename in files %}1499 <li class="file {{ row_class.next() }}">{{ filename|e }}</li>1497 {% for file in files %}1498 <li class="file {{ row_class.next() }}">{{ file }}1499 {% endfor %}1500 </ul>15011503 A cycler has the following attributes and methods:1502 :param items: Each positional argument will be yielded in the order1503 given for each cycle.15041505 .. method:: reset()1505 .. versionadded:: 2.115061507 Resets the cycle to the first item.1507 .. method:: current1508 :property:15091510 Return the current item. Equivalent to the item that will be1511 returned next time :meth:`next` is called.15121513 .. method:: next()15141511 Goes one item ahead and returns the then-current item.1515 Return the current item, then advance :attr:`current` to the1516 next item.15171513 .. attribute:: current1518 .. method:: reset()15191515 Returns the current item.1520 Resets the current item to the first item.15211517 .. versionadded:: 2.115181522.. class:: joiner(sep=', ')15231524 A tiny helper that can be used to "join" multiple sections. A joiner is1525 passed a string and will return that string every time it's called, except
295 unmodified lines
modified:jinja2/utils.py+32 added-6 removed
564 unmodified lines
565 .replace(u"'", u'\\u0027')566 return Markup(rv)567568569@implements_iterator569class Cycler(object):571 """A cycle helper for templates."""570 """Cycle through values by yield them one at a time, then restarting571 once the end is reached. Available as ``cycler`` in templates.572573 Similar to ``loop.cycle``, but can be used outside loops or across574 multiple loops. For example, render a list of folders and files in a575 list, alternating giving them "odd" and "even" classes.576577 .. code-block:: html+jinja578579 {% set row_class = cycler("odd", "even") %}580 <ul class="browser">581 {% for folder in folders %}582 <li class="folder {{ row_class.next() }}">{{ folder }}583 {% endfor %}584 {% for file in files %}585 <li class="file {{ row_class.next() }}">{{ file }}586 {% endfor %}587 </ul>588589 :param items: Each positional argument will be yielded in the order590 given for each cycle.591592 .. versionadded:: 2.1593 """594595 def __init__(self, *items):596 if not items:597 raise RuntimeError('at least one item has to be provided')598 self.items = items577 self.reset()599 self.pos = 0600601 def reset(self):580 """Resets the cycle."""602 """Resets the current item to the first item."""603 self.pos = 0604605 @property606 def current(self):585 """Returns the current item."""607 """Return the current item. Equivalent to the item that will be608 returned next time :meth:`next` is called.609 """610 return self.items[self.pos]611612 def next(self):589 """Goes one item ahead and returns it."""613 """Return the current item, then advance :attr:`current` to the614 next item.615 """616 rv = self.current617 self.pos = (self.pos + 1) % len(self.items)618 return rv619
50 unmodified lines