4

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
starmonkey
  • 3,147
  • 2
  • 20
  • 15
  • I like the name _zebra striping_. Never heard before and makes perfect sense. – Pindatjuh May 21 '11 at 09:53
  • Note that you can handle this in CSS3, which is advantageous as it leaves design up to the designers: tr:nth-child(odd) { background-color: #99ff99; } – starmonkey Nov 02 '11 at 01:31

2 Answers2

3

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

<td tal:attributes="class evenodd:repeat/item/odd">
Kornel
  • 97,764
  • 37
  • 219
  • 309
2

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
  • 3,147
  • 2
  • 20
  • 15