5.3.8 <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.8.8) identified in the local namespace by the iter (5.3.8.5) attribute to iterate over the sequence defined by the expression in the expr (5.3.8.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.8.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        False         True     0     0     0     0
       3        False         True     1     0     1     1
       3        False         True     2     0     2     2
pagesize has_prevpage has_nextpage index start count value
----------------------------------------------------------
       3         True         True     3     3     0     3
       3         True         True     4     3     1     4
       3         True         True     5     3     2     5
pagesize has_prevpage has_nextpage index start count value
----------------------------------------------------------
       3         True        False     6     6     0     6
       3         True        False     7     6     1     7
       3         True        False     8     6     2     8



Subsections