5.3.7 <al-for>
Description: <al-for>
Keywords: albatross

##
5.3.7 `<al-for>`

This tag implements a loop in almost the same way as the `for` keyword in Python.

The tag uses an instance of the `ListIterator` (5.3.7.8) identified in the local namespace by the `iter` (5.3.7.5) attribute to iterate over the sequence defined by the expression in the `expr` (5.3.7.3) attribute.

    >>> import albatross
    >>> ctx = albatross.SimpleContext('.')
    >>> albatross.Template(ctx, '<magic>', '''
    ... <al-for iter="i" expr="range(15)" whitespace="indent">
    ...  <al-value expr="i.value()">
    ... </al-for whitespace>
    ... ''').to_html(ctx)
    >>> ctx.flush_content()
     0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Note that you must use the `value()` method of the iterator to retrieve the current sequence value.

When using pagination mode via the `pagesize` (5.3.7.6) attribute the `prevpage` and `nextpage` attributes of the `<al-input>` (5.2.2) and `<al-a>` (5.2.6) tags can be used to automatically page forwards and backwards through a sequence.

The following simulates pagination via the `set_backdoor()` `ListIterator` method and shows other data that is maintained by the iterator.

    >>> import albatross
    >>> ctx = albatross.SimpleContext('.')
    >>> ctx.locals.seq = range(9)
    >>> t = albatross.Template(ctx, '<magic>', '''
    ... pagesize has_prevpage has_nextpage index start count value
    ... ----------------------------------------------------------
    ... <al-for iter="i" expr="seq" pagesize="3">
    ... <al-value expr="'%8s' % i.pagesize()">
    ... <al-value expr="'%13s' % i.has_prevpage()">
    ... <al-value expr="'%13s' % i.has_nextpage()">
    ... <al-value expr="'%6s' % i.index()">
    ... <al-value expr="'%6s' % i.start()">
    ... <al-value expr="'%6s' % i.count()">
    ... <al-value expr="'%6s' % i.value()" whitespace>
    ... </al-for>''')
    >>> t.to_html(ctx)
    >>> ctx.locals.i.set_backdoor('nextpage', 'nextpage,i')
    >>> t.to_html(ctx)
    >>> ctx.locals.i.set_backdoor('nextpage', 'nextpage,i')
    >>> t.to_html(ctx)
    >>> ctx.flush_content()
    pagesize has_prevpage has_nextpage index start count value
    ----------------------------------------------------------
           3            0            1     0     0     0     0
           3            0            1     1     0     1     1
           3            0            1     2     0     2     2
    pagesize has_prevpage has_nextpage index start count value
    ----------------------------------------------------------
           3            1            1     3     3     0     3
           3            1            1     4     3     1     4
           3            1            1     5     3     2     5
    pagesize has_prevpage has_nextpage index start count value
    ----------------------------------------------------------
           3            1            0     6     6     0     6
           3            1            0     7     6     1     7
           3            1            0     8     6     2     8

Subsections

- 5.3.7.1 `cols="..."` attribute
- 5.3.7.2 `continue` attribute
- 5.3.7.3 `expr="..."` attribute
- 5.3.7.4 `flow="..."` attribute
- 5.3.7.5 `iter="..."` attribute
- 5.3.7.6 `pagesize="..."` attribute
- 5.3.7.7 `prepare` attribute
- 5.3.7.8 ListIterator Objects