|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
naish Smarty n00b
Joined: 13 May 2003 Posts: 4
|
Posted: Tue May 13, 2003 10:09 am Post subject: Extending html_checkboxes |
|
|
Hello everyone
I just extended to html_checkboxes with a new option var. If this option is set the function will assign its output to the variable named by var. This is also potentially useful for html_radios.
The output is a standard array suitable for use in e.g. html_table.
I would like everyones opinion on the extension and if it would be worth including into the standard smarty distribution?`
Thoughts and comments.
Michael.
PS. This is the code with my quick'n'dirty hack:
[php:1:519a06e251]
function smarty_function_html_checkboxes_formatted($params, &$smarty)
{
require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
$name = 'checkbox';
$values = null;
$options = null;
$selected = null;
$separator = '';
$output = null;
$var='';
$extra = '';
foreach($params as $_key => $_val) {
switch($_key) {
case 'name':
case 'separator':
case 'var':
$$_key = $_val;
break;
case 'options':
$$_key = (array)$_val;
break;
case 'values':
case 'output':
$$_key = array_values((array)$_val);
break;
case 'checked':
case 'selected':
$selected = array_values((array)$_val);
break;
case 'checkboxes':
$smarty->trigger_error('html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead', E_USER_WARNING);
$options = (array)$_val;
break;
default:
$extra .= ' '.$_key.'="'.smarty_function_escape_special_chars((string)$_val).'"';
break;
}
}
if (!isset($options) && !isset($values))
return ''; /* raise error here? */
settype($selected, 'array');
$_html_result = '';
$_temp='';
if($var!='')
$result=array();
if (is_array($options)) {
foreach ($options as $_key=>$_val)
{
$_temp .= smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator);
if($var!='')
$result[sizeof($result)]=$_temp;
else
$_html_result.=$_temp;
}
} else {
foreach ($values as $_i=>$_key) {
$_val = isset($output[$_i]) ? $output[$_i] : '';
$_temp = smarty_function_html_checkboxes_output($name, $_key, $_val, $selected, $extra, $separator);
if($var!='')
$result[sizeof($result)]=$_temp;
else
$_html_result.=$_temp;
}
}
if($var!='')
$smarty->assign($var,$result);
else
return $_html_result;
return;
}
[/php:1:519a06e251] |
|
Back to top |
|
andre Smarty Pro
Joined: 23 Apr 2003 Posts: 164 Location: Karlsruhe, Germany
|
Posted: Tue May 13, 2003 10:34 am Post subject: |
|
|
Normally smarty uses the parameter "assign" to assign a value to a variable instead of printing it. Pehaps you should rename your parameter "var" to be more similar to other smarty functions.
{html_checkboxes assign="myArray" ... } |
|
Back to top |
|
naish Smarty n00b
Joined: 13 May 2003 Posts: 4
|
Posted: Tue May 13, 2003 11:11 am Post subject: |
|
|
Good point, I shall do so immediatly. Besides there is another small bug I would like to fix.
Michael |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sat May 24, 2003 7:09 am Post subject: |
|
|
@andre: isn't it interesting though that the exception to the rule is the {assign} plugin?
I should think that it would be better to deprecate {assign} and instead favour the following:
{var assign=var value=val}
It is still the exact same amount of typing (grrr) but at least it uses consistent naming within the template. I don't really see the need to follow the API names when in template -- they are separate worlds, after all.
What do you think? |
|
Back to top |
|
andre Smarty Pro
Joined: 23 Apr 2003 Posts: 164 Location: Karlsruhe, Germany
|
Posted: Mon May 26, 2003 9:02 am Post subject: |
|
|
Mhmm... yeah, {assign} is an exception. But doesn't a exception proves the rule
I'm not really sure if {assign} should be renamed because assigning is exactly that what it does. So what does {var} mean?!
I wouldn't change it
bye,
André |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|