Smarty plugin for zebra tables

This is probably one of that classical piece of software you should never use, but I find it useful in more than one occasion, so…

It’s a simple plugin for Smarty template engine that apply a different class to even and odd rows of an (x)HTML table.

function smarty_block_alternate_row($params, $content, &$smarty)
{
  if (is_null($content)) {
    return;
  }
  
  $rows = preg_split('/(<tr)/', $content, -1 ,PREG_SPLIT_DELIM_CAPTURE );

  for($i=0, $j=1; $i < count($rows); $i++) {
    if ($rows[$i] == '<tr') {
      $rows[$i] = '<tr class="' . ($j%2==0?$params['class_for_even']:$params['class_for_odd']) . '"';
      $j++;
    }
  }
  $content = implode('', $rows);

  return $content;
}

Post your comment

You can use Textile





0 comments

Readers left 0 notes on this photograph. Live yours using the form below.

Published on
14 January 2005
At
14:08
Tagged
php, Article