Smarty表甚至奇怪

xer*_*ool 4 php smarty

我正在使用PHP smarty templater.我需要创建甚至奇数行突出显示.请给我示例如何做到这一点.

我也有变量:

$smarty.foreach.product.index
Run Code Online (Sandbox Code Playgroud)

小智 12

对于这种情况,smarty有一种称为{cycle}的方法

<table>
{foreach $products as $product}
<tr class="{cycle values="odd,even"}">
   <td>{$product.name}</td>
</tr>
{/foreach}
</table>
Run Code Online (Sandbox Code Playgroud)

结果将是:

<table>
<tr class="odd">
   <td>1st product</td>
</tr>
<tr class="even">
   <td>2nd product</td>
</tr>
<tr class="odd">
   <td>3rd product</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)

在样式表文件中定义奇数和偶数行的属性,如下所示:

tr.even td{background: #CCCCCC;}
tr.odd td{background: #EFEFEF;}
Run Code Online (Sandbox Code Playgroud)