如何用TAL创建zebra-stripe CSS?

问题描述 投票:43回答:1

如何使用Chameleon或Zope页面模板轻松创建CSS斑马条纹?我想将oddeven类添加到表中的每一行,但使用repeat/name/oddrepeat/name/even的条件看起来相当冗长,即使使用条件表达式:

<table>
   <tr tal:repeat="row rows" 
       tal:attributes="class python:repeat['row'].odd and 'odd' or 'even'">
       <td tal:repeat="col row" tal:content="col">column text text</td>
   </tr>
</table>

如果您有多个要计算的类,这将变得特别繁琐。

css zope template-tal chameleon zpt
1个回答
33
投票

repeat变量的Zope页面模板实现有一个记录不足的额外参数parity,而不是给你字符串'odd''even',在迭代之间交替:

<table>
   <tr tal:repeat="row rows" 
       tal:attributes="class repeat/row/parity">
       <td tal:repeat="col row" tal:content="col">column text text</td>
   </tr>
</table>

插入字符串表达式也更容易:

tal:attributes="class string:striped ${row/class} ${repeat/row/parity}"

这也适用于变色龙。

© www.soinside.com 2019 - 2024. All rights reserved.