mirror of
https://github.com/smarty-php/smarty.git
synced 2025-10-25 02:11:37 +02:00
html_options, html_checkboxes and html_radios now pass-thru all unknown paramters
This commit is contained in:
@@ -5,71 +5,96 @@
|
||||
* -------------------------------------------------------------
|
||||
* Type: function
|
||||
* Name: html_options
|
||||
* Input: name (optional) - string default "select"
|
||||
* values (required if no options supplied) - array
|
||||
* options (required if no values supplied) - associative array
|
||||
* selected (optional) - string default not set
|
||||
* output (required if not options supplied) - array
|
||||
* Purpose: Prints the list of <option> tags generated from
|
||||
* the passed parameters
|
||||
* -------------------------------------------------------------
|
||||
*/
|
||||
function smarty_function_html_options($params, &$smarty)
|
||||
{
|
||||
require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
|
||||
require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
|
||||
|
||||
$name = 'select';
|
||||
$values = null;
|
||||
$options = null;
|
||||
$selected = array();
|
||||
$output = null;
|
||||
|
||||
extract($params);
|
||||
$extra = '';
|
||||
|
||||
foreach($params as $_key => $_val) {
|
||||
switch($_key) {
|
||||
case 'name':
|
||||
$$_key = (string)$_val;
|
||||
break;
|
||||
|
||||
$html_result = '';
|
||||
case 'options':
|
||||
$$_key = (array)$_val;
|
||||
break;
|
||||
|
||||
settype($selected, 'array');
|
||||
if (isset($options)) {
|
||||
settype($options, 'array');
|
||||
foreach ($options as $key => $value) {
|
||||
$html_result .= smarty_function_html_options_optoutput($key, $value, $selected);
|
||||
}
|
||||
} else {
|
||||
settype($output, 'array');
|
||||
settype($values, 'array');
|
||||
for ($i = 0, $for_max = count($output); $i < $for_max; $i++) {
|
||||
if ($i < count($values)) {
|
||||
$html_result .= smarty_function_html_options_optoutput($values[$i], $output[$i], $selected);
|
||||
} else {
|
||||
$html_result .= smarty_function_html_options_optoutput($output[$i], $output[$i], $selected);
|
||||
}
|
||||
}
|
||||
}
|
||||
case 'selected':
|
||||
case 'values':
|
||||
case 'output':
|
||||
$$_key = array_values((array)$_val);
|
||||
break;
|
||||
|
||||
/* passthru all unknown params to $extra */
|
||||
$extra = '';
|
||||
unset($params['values']);
|
||||
unset($params['output']);
|
||||
unset($params['selected']);
|
||||
unset($params['options']);
|
||||
unset($params['name']);
|
||||
foreach ($params as $key=>$value) $extra .= ' ' . $key . '="'. htmlspecialchars($value) . '"';
|
||||
default:
|
||||
$extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isset($options) && !isset($values))
|
||||
return ''; /* raise error here? */
|
||||
|
||||
$_html_result = '';
|
||||
|
||||
if (is_array($options)) {
|
||||
|
||||
foreach ($options as $_key=>$_val)
|
||||
$_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
|
||||
|
||||
} else {
|
||||
|
||||
foreach ((array)$values as $_i=>$_key) {
|
||||
$_val = isset($output[$_i]) ? $output[$_i] : '';
|
||||
$_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(!empty($name)) {
|
||||
$_html_result = '<select name="' . $name . '"' . $extra . '>' . "\n" . $_html_result . '</select>' . "\n";
|
||||
}
|
||||
|
||||
return $_html_result;
|
||||
|
||||
if(!empty($name)) {
|
||||
$html_result = '<select name="' . $name . '"' . $extra . '>' . "\n" . $html_result . '</select>' . "\n";
|
||||
}
|
||||
return $html_result;
|
||||
}
|
||||
|
||||
function smarty_function_html_options_optoutput($key, $value, $selected) {
|
||||
if(!is_array($value)) {
|
||||
$html_result = '<option label="' . smarty_function_escape_special_chars($value) . '" value="' .
|
||||
smarty_function_escape_special_chars($key) . '"';
|
||||
if (in_array($key, $selected))
|
||||
$html_result .= " selected=\"selected\"";
|
||||
$html_result .= '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n";
|
||||
} else {
|
||||
$html_result = smarty_function_html_options_optgroup($key, $value, $selected);
|
||||
}
|
||||
return $html_result;
|
||||
if(!is_array($value)) {
|
||||
$_html_result = '<option label="' . smarty_function_escape_special_chars($value) . '" value="' .
|
||||
smarty_function_escape_special_chars($key) . '"';
|
||||
if (in_array($key, $selected))
|
||||
$_html_result .= ' selected="selected"';
|
||||
$_html_result .= '>' . smarty_function_escape_special_chars($value) . '</option>' . "\n";
|
||||
} else {
|
||||
$_html_result = smarty_function_html_options_optgroup($key, $value, $selected);
|
||||
}
|
||||
return $_html_result;
|
||||
}
|
||||
|
||||
function smarty_function_html_options_optgroup($key, $values, $selected) {
|
||||
$optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($value) . '">' . "\n";
|
||||
foreach ($values as $key => $value) {
|
||||
$optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected);
|
||||
}
|
||||
$optgroup_html .= "</optgroup>\n";
|
||||
return $optgroup_html;
|
||||
$optgroup_html = '<optgroup label="' . smarty_function_escape_special_chars($value) . '">' . "\n";
|
||||
foreach ($values as $key => $value) {
|
||||
$optgroup_html .= smarty_function_html_options_optoutput($key, $value, $selected);
|
||||
}
|
||||
$optgroup_html .= "</optgroup>\n";
|
||||
return $optgroup_html;
|
||||
}
|
||||
|
||||
/* vim: set expandtab: */
|
||||
|
||||
Reference in New Issue
Block a user