This is a macro that automatically generates an HTML table from a list of lists.
macro ( 'AutoTable', lambda data, header=False: (
assign ( 'alts', [ 'even', 'odd' ] ),
data and (
T.table ( class_='autotable' ) [
header and (
T.thead [ [ T.th [ _col ] for _col in data [ 0 ] ] ]
),
T.tbody [
[ T.tr ( class_='row-%s' % alts [ _rx % 2 ] ) [
[ T.td ( class_='col-%s' % alts [ _cx % 2 ] ) [ _col ]
for _cx, _col in enumerate ( _row ) ]
] for _rx, _row in enumerate ( data [ int ( header ): ] ) ]
]
]
)
) )
Assuming you have a list of lists like this:
data = [
[ 'A', 'B', 'C', 'D' ],
range ( 0, 4 ),
range ( 4, 8 ),
range ( 8, 12 )
]
and a template like this:
html [
head [ title [ 'AutoTable Demo' ] ],
body [
AutoTable ( data, header=True )
]
]
AutoTable would generate the following HTML:
<html>
<head><title>AutoTable Demo</title></head>
<body>
<table class="autotable">
<thead>
<th>A</th><th>B</th><th>C</th><th>D</th>
</thead>
<tbody>
<tr class="row-even">
<td class="col-even">0</td>
<td class="col-odd">1</td>
<td class="col-even">2</td>
<td class="col-odd">3</td>
</tr>
<tr class="row-odd">
<td class="col-even">4</td>
<td class="col-odd">5</td>
<td class="col-even">6</td>
<td class="col-odd">7</td>
</tr>
<tr class="row-even">
<td class="col-even">8</td>
<td class="col-odd">9</td>
<td class="col-even">10</td>
<td class="col-odd">11</td>
</tr>
</tbody>
</table>
</body>
</html>