Commit | Line | Data |
---|---|---|
e586807d H |
1 | <?php |
2 | /** | |
3 | * Smarty plugin | |
4 | * @package Smarty | |
5 | * @subpackage plugins | |
6 | */ | |
7 | ||
8 | ||
9 | /** | |
10 | * Smarty {html_table} function plugin | |
11 | * | |
12 | * Type: function<br> | |
13 | * Name: html_table<br> | |
14 | * Date: Feb 17, 2003<br> | |
15 | * Purpose: make an html table from an array of data<br> | |
16 | * Input:<br> | |
17 | * - loop = array to loop through | |
18 | * - cols = number of columns | |
19 | * - rows = number of rows | |
20 | * - table_attr = table attributes | |
21 | * - tr_attr = table row attributes (arrays are cycled) | |
22 | * - td_attr = table cell attributes (arrays are cycled) | |
23 | * - trailpad = value to pad trailing cells with | |
24 | * - vdir = vertical direction (default: "down", means top-to-bottom) | |
25 | * - hdir = horizontal direction (default: "right", means left-to-right) | |
26 | * - inner = inner loop (default "cols": print $loop line by line, | |
27 | * $loop will be printed column by column otherwise) | |
28 | * | |
29 | * | |
30 | * Examples: | |
31 | * <pre> | |
32 | * {table loop=$data} | |
33 | * {table loop=$data cols=4 tr_attr='"bgcolor=red"'} | |
34 | * {table loop=$data cols=4 tr_attr=$colors} | |
35 | * </pre> | |
36 | * @author Monte Ohrt <monte@ispi.net> | |
37 | * @version 1.0 | |
38 | * @link http://smarty.php.net/manual/en/language.function.html.table.php {html_table} | |
39 | * (Smarty online manual) | |
40 | * @param array | |
41 | * @param Smarty | |
42 | * @return string | |
43 | */ | |
44 | function smarty_function_html_table($params, &$smarty) | |
45 | { | |
46 | $table_attr = 'border="1"'; | |
47 | $tr_attr = ''; | |
48 | $td_attr = ''; | |
49 | $cols = 3; | |
50 | $rows = 3; | |
51 | $trailpad = ' '; | |
52 | $vdir = 'down'; | |
53 | $hdir = 'right'; | |
54 | $inner = 'cols'; | |
55 | ||
56 | extract($params); | |
57 | ||
58 | if (!isset($loop)) { | |
59 | $smarty->trigger_error("html_table: missing 'loop' parameter"); | |
60 | return; | |
61 | } | |
62 | ||
63 | $loop_count = count($loop); | |
64 | if (empty($params['rows'])) { | |
65 | /* no rows specified */ | |
66 | $rows = ceil($loop_count/$cols); | |
67 | } elseif (empty($params['cols'])) { | |
68 | if (!empty($params['rows'])) { | |
69 | /* no cols specified, but rows */ | |
70 | $cols = ceil($loop_count/$rows); | |
71 | } | |
72 | } | |
73 | ||
74 | $output = "<table $table_attr>\n"; | |
75 | ||
76 | for ($r=0; $r<$rows; $r++) { | |
77 | $output .= "<tr" . smarty_function_html_table_cycle('tr', $tr_attr, $r) . ">\n"; | |
78 | $rx = ($vdir == 'down') ? $r*$cols : ($rows-1-$r)*$cols; | |
79 | ||
80 | for ($c=0; $c<$cols; $c++) { | |
81 | $x = ($hdir == 'right') ? $rx+$c : $rx+$cols-1-$c; | |
82 | if ($inner!='cols') { | |
83 | /* shuffle x to loop over rows*/ | |
84 | $x = floor($x/$cols) + ($x%$cols)*$rows; | |
85 | } | |
86 | ||
87 | if ($x<$loop_count) { | |
88 | $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">" . $loop[$x] . "</td>\n"; | |
89 | } else { | |
90 | $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">$trailpad</td>\n"; | |
91 | } | |
92 | } | |
93 | $output .= "</tr>\n"; | |
94 | } | |
95 | $output .= "</table>\n"; | |
96 | ||
97 | return $output; | |
98 | } | |
99 | ||
100 | function smarty_function_html_table_cycle($name, $var, $no) { | |
101 | if(!is_array($var)) { | |
102 | $ret = $var; | |
103 | } else { | |
104 | $ret = $var[$no % count($var)]; | |
105 | } | |
106 | ||
107 | return ($ret) ? ' '.$ret : ''; | |
108 | } | |
109 | ||
110 | ||
111 | /* vim: set expandtab: */ | |
112 | ||
113 | ?> |