• 交替风格
    • 问题
    • 方案
  • code.py
  • template.html
  • New Templetor

    交替风格

    问题

    你想通过数据集合动态的生成交替背景色的列表.

    方案

    Give templetor access to the int built-in and use modulo to test.

    code.py

    1. web.template.Template.globals['int'] = int

    template.html

    1. <ul>
    2. $var i: 0
    3. $for track in tracks:
    4. $var i: ${int(self.i) + 1}
    5. <li class="
    6. $if int(self.i) % 2:
    7. odd
    8. $else:
    9. even
    10. ">$track.title</li>
    11. </ul>

    New Templetor

    In the new implementation of templetor (which will be the default when version .3 is released), within any template loop you have access to a $loop variable. This works like so:

    1. <ul>
    2. $for foo in foos:
    3. <li class="$loop.parity">
    4. $foo
    5. </li>
    6. </ul>