init
[garradin.git] / include / libs / template_lite / plugins / function.html_table.php
1 <?php
2
3 /*
4 * Template Lite plugin
5 * -------------------------------------------------------------
6 * Type: function
7 * Name: html_table
8 * Version: 1.0
9 * Date: Feb 17, 2003
10 * Author: Monte Ohrt <monte@ispi.net>
11 * Purpose: make an html table from an array of data
12 * Input: loop = array to loop through
13 * cols = number of columns
14 * table_attr = table attributes
15 * tr_attr = table row attributes (arrays are cycled)
16 * td_attr = table cell attributes (arrays are cycled)
17 * trailpad = value to pad trailing cells with
18 *
19 * Examples: {table loop=$data}
20 * {$table loop=$data cols=4 tr_attr='"bgcolor=red"'}
21 * {$table loop=$data cols=4 tr_attr=$colors}
22 * Taken from the original Smarty
23 * http://smarty.php.net
24 * -------------------------------------------------------------
25 */
26 function tpl_function_html_table($params, &$template_object)
27 {
28 $table_attr = 'border="1"';
29 $tr_attr = '';
30 $td_attr = '';
31 $cols = 3;
32 $trailpad = '&nbsp;';
33
34 extract($params);
35
36 if (!isset($loop))
37 {
38 throw new Template_Exception("html_table: missing 'loop' parameter", $template_object);
39 return;
40 }
41
42 $output = "<table $table_attr>\n";
43 $output .= "<tr " . tpl_function_html_table_cycle('tr', $tr_attr) . ">\n";
44
45 for($x = 0, $y = count($loop); $x < $y; $x++)
46 {
47 $output .= "<td " . tpl_function_html_table_cycle('td', $td_attr) . ">" . $loop[$x] . "</td>\n";
48 if((!(($x+1) % $cols)) && $x < $y-1)
49 {
50 // go to next row
51 $output .= "</tr>\n<tr " . tpl_function_html_table_cycle('tr', $tr_attr) . ">\n";
52 }
53 if($x == $y-1)
54 {
55 // last row, pad remaining cells
56 $cells = $cols - $y % $cols;
57 if($cells != $cols) {
58 for($padloop = 0; $padloop < $cells; $padloop++) {
59 $output .= "<td " . tpl_function_html_table_cycle('td', $td_attr) . ">$trailpad</td>\n";
60 }
61 }
62 $output .= "</tr>\n";
63 }
64 }
65 $output .= "</table>\n";
66 return $output;
67 }
68
69 function tpl_function_html_table_cycle($name, $var)
70 {
71 static $names = array();
72
73 if(!is_array($var))
74 {
75 return $var;
76 }
77
78 if(!isset($names[$name]) || $names[$name] == count($var)-1)
79 {
80 $names[$name] = 0;
81 return $var[0];
82 }
83
84 $names[$name]++;
85 return $var[$names[$name]];
86 }
87
88 ?>