tags:

views:

423

answers:

2

I'm trying out PHPTAL and I want to render a table with zebra stripes. I'm looping through a simple php assoc array ($_SERVER).

Note that I don't want to use jQuery or anything like that, I'm trying to learn PHPTAL usage!

Currently I have it working like this (too verbose for my liking):

<tr tal:repeat="item server">
  <td tal:condition="repeat/item/odd" tal:content="repeat/item/key" class="odd">item key</td>
  <td tal:condition="repeat/item/even" tal:content="repeat/item/key" class="even">item key</td>
  <td tal:condition="repeat/item/odd" tal:content="item" class="odd">item value</td>
  <td tal:condition="repeat/item/even" tal:content="item" class="even">item value</td>
</tr>

Basically I want some kind of conditional assignment on the fly, but I'm unsure of the syntax.

+1  A: 

Well, it seems like I have my own answer, though I still think this is rather ugly:

<tr tal:repeat="item server">
  <td tal:content="repeat/item/key" tal:attributes="class php: repeat.item.odd ? 'odd' : 'even'">item key</td>
  <td tal:content="item" tal:attributes="class php: repeat.item.odd ? 'odd' : 'even'">item value</td>
</tr>

Anyone got anything more graceful looking for PHPTAL?

starmonkey
+2  A: 

You could create expression modifier by writing phptal_tales_evenodd() function (see phptal_tales() in manual):

<td tal:attributes="class evenodd:repeat/item/odd">
porneL