Rendering ordered lists in page templates
=========================================

schooltool.app defines a TALES path adapter ("thingy" for short) that lets
you sort lists of objects directly in a page template by writing, for example,
the following:

    <tr tal:repeat="item context/values/sortby:title">
      <td tal:content="item/title" />
    </tr>

Boring (or interesting, depending on your perspective) implementation details
-----------------------------------------------------------------------------

We register a path adapter 'sortby' and it becomes available in TALES
path expressions.

    >>> from zope.app.testing import setup
    >>> setup.placelessSetUp()

    >>> from zope.component import provideAdapter
    >>> from zope.traversing.interfaces import IPathAdapter
    >>> from schooltool.app.browser import SortBy
    >>> provideAdapter(SortBy, name='sortby', provides=IPathAdapter)
    >>> from zope.security.checker import defineChecker
    >>> from zope.security.checker import NamesChecker
    >>> defineChecker(SortBy, NamesChecker(['traverse']))

Suppose we have a sequence of dicts that have a 'title' key.

    >>> a_list = [{'id': 42, 'title': 'How to get ahead in navigation'},
    ...           {'id': 11, 'title': 'The ultimate answer: 6 * 9'},
    ...           {'id': 33, 'title': 'Alphabet for beginners'}]

We can sort the list by title with TALES:

    >>> from zope.pagetemplate.engine import Engine
    >>> from zope.tales.tales import Context
    >>> context = Context(Engine, {'a_list': a_list})

    >>> bytecode = Engine.compile('a_list/sortby:title')
    >>> for item in bytecode(context):
    ...     print item['title']
    Alphabet for beginners
    How to get ahead in navigation
    The ultimate answer: 6 * 9

You can also sort lists of objects by attribute.  You can sort arbitrary
iterables.  See the implementation and tests of `SortBy` for exhaustive
examples.

    >>> setup.placelessTearDown()

