10 * Smarty {html_options} function plugin
13 * Name: html_options<br>
15 * - name (optional) - string default "select"
16 * - values (required if no options supplied) - array
17 * - options (required if no values supplied) - associative array
18 * - selected (optional) - string default not set
19 * - output (required if not options supplied) - array
20 * Purpose: Prints the list of <option> tags generated from
21 * the passed parameters
22 * @link http://smarty.php.net/manual/en/language.function.html.options.php {html_image}
23 * (Smarty online manual)
24 * @author Monte Ohrt <monte at ohrt dot com>
28 * @uses smarty_function_escape_special_chars()
30 function smarty_function_html_options($params, &$smarty)
32 require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
42 foreach($params as $_key => $_val) {
45 $
$_key = (string)$_val;
49 $
$_key = (array)$_val;
54 $
$_key = array_values((array)$_val);
58 $
$_key = array_map('strval', array_values((array)$_val));
62 if(!is_array($_val)) {
63 $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
65 $smarty->trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE
);
71 if (!isset($options) && !isset($values))
72 return ''; /* raise error here? */
76 if (isset($options)) {
78 foreach ($options as $_key=>$_val)
79 $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
83 foreach ($values as $_i=>$_key) {
84 $_val = isset($output[$_i]) ?
$output[$_i] : '';
85 $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
91 $_html_result = '<select name="' . $name . '"' . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
98 function smarty_function_html_options_optoutput($key, $value, $selected) {
99 if(!is_array($value)) {
100 $_html_result = '<option label="' . smarty_function_escape_special_chars($value) . '" value="' .
101 smarty_function_escape_special_chars($key) . '"';
102 if (in_array((string)$key, $selected))
103 $_html_result .= ' selected="selected"';
104 $_html_result .= '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n";
106 $_html_result = smarty_function_html_options_optgroup($key, $value, $selected);
108 return $_html_result;
111 function smarty_function_html_options_optgroup($key, $values, $selected) {
112 $optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($key) . '">' . "\n";
113 foreach ($values as $key => $value) {
114 $optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected);
116 $optgroup_html .= "</optgroup>\n";
117 return $optgroup_html;
120 /* vim: set expandtab: */
This page took 0.511266 seconds and 4 git commands to generate.