Google App Engine用フレームワーク「Kay」でテーブルをストライプにする

Google App Engine用フレームワーク「Kay」でテーブルをストライプにする」より。

Google App EngineフレームワークKay」でテーブルをストライプにする方法。

Kay」ではテンプレートにJinja2を使用しています。

Jinja2のヘルパー関数「loop.cycle」を使用すると、簡単にテーブルをストライプにできます。

<table>
{% for row in rows %}
<tr class="{{ loop.cycle('odd', 'even') }}">
<td>…</td>

</tr>
{% endfor %}
</table>

loop.cycleの引数には、2つに限りません。

3つ以上の引数を指定することもできます。

<table>
{% for row in rows %}
<tr class="{{ loop.cycle('line1', 'line2', 'line3', 'line4') }}">

<td>…</td>
</tr>
{% endfor %}
</table>