Zebra Tables In Zend Framework

I came across this today at work:

<?php $rowClass = '#f0f0f0' ?>
<table>
<?php foreach($this->books as $book) { ?>
   <?php $i = 0; ?>
   <tr class = "<?php echo $rowClass ?>">
      <td><?php echo $book['author'] ?></td>
   </tr>
   <?php
      if ($i % 2 == 0)
         $rowClass = '#ffffff';
      else
         $rowClass = '#f0f0f0';
      $i++;
    ?>
<?php } ?>
</table>

Zebra tables! Written like a five year old!

For a split second I longed for Smarty’s {cycle} function. Does ZF have something similar? Well of course it does, and it’s even has the word ‘cycle’ in it!

<? $cycle=$this->cycle(array("#F0F0F0","#FFFFFF"));?>
<? foreach ($this->books as $book):?>
  <tr style="background-color:<?=$cycle->next()?>">
  <td><?=$this->escape($book['author']) ?></td>
</tr>
<? endforeach;?>

As a sidenote: ZF’s view helpers are so_much_cleaner than Smarty’s custom functions…

Comments

One Response to “Zebra Tables In Zend Framework”

  1. driver42 on October 22nd, 2009 2:27 pm

    Using metaphorical straws helps prevent injuries to real camels. ,

Leave a Reply