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
Provenance
- Commit
29d1ff4- Tree
dd3d4ab- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
1b3d6ed- Parent 2
cde8a42
2 files changed+54 added−25 removed
modified:docs/templates.rst+22 added-19 removed
1478 unmodified lines1479 as ``dict(foo='bar')``.1480 1481 .. class:: cycler(\*items)1482 1483- 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.1485 1487- 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.1489 1491- The following example shows how `cycler` can be used::1490+ .. code-block:: html+jinja1491 1493- {% 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>1501 1503- A cycler has the following attributes and methods:1502+ :param items: Each positional argument will be yielded in the order1503+ given for each cycle.1504 1505- .. method:: reset()1505+ .. versionadded:: 2.11506 1507- Resets the cycle to the first item.1507+ .. method:: current1508+ :property:1509 1510+ Return the current item. Equivalent to the item that will be1511+ returned next time :meth:`next` is called.1512+1513 .. method:: next()1514 1511- Goes one item ahead and returns the then-current item.1515+ Return the current item, then advance :attr:`current` to the1516+ next item.1517 1513- .. attribute:: current1518+ .. method:: reset()1519 1515- Returns the current item.1520+ Resets the current item to the first item.1521 1517- .. versionadded:: 2.11518-1522 .. class:: joiner(sep=', ')1523 1524 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, except295 unmodified linesmodified:jinja2/utils.py+32 added-6 removed
564 unmodified lines565 .replace(u"'", u'\\u0027')566 return Markup(rv)567 568 569-@implements_iterator569 class 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.572 573+ 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.576+577+ .. code-block:: html+jinja578+579+ {% 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>588+589+ :param items: Each positional argument will be yielded in the order590+ given for each cycle.591+592+ .. versionadded:: 2.1593+ """594+595 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 = 0600 601 def reset(self):580- """Resets the cycle."""602+ """Resets the current item to the first item."""603 self.pos = 0604 605 @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]611 612 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