Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Extending html_checkboxes

 
Post new topic   Reply to topic    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
naish
Smarty n00b


Joined: 13 May 2003
Posts: 4

PostPosted: Tue May 13, 2003 10:09 am    Post subject: Extending html_checkboxes Reply with quote

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
View user's profile Send private message
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Tue May 13, 2003 10:34 am    Post subject: Reply with quote

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
View user's profile Send private message
naish
Smarty n00b


Joined: 13 May 2003
Posts: 4

PostPosted: Tue May 13, 2003 11:11 am    Post subject: Reply with quote

Good point, I shall do so immediatly. Besides there is another small bug I would like to fix.

Michael
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sat May 24, 2003 7:09 am    Post subject: Reply with quote

@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
View user's profile Send private message
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Mon May 26, 2003 9:02 am    Post subject: Reply with quote

Mhmm... yeah, {assign} is an exception. But doesn't a exception proves the rule Wink

I'm not really sure if {assign} should be renamed because assigning is exactly that what it does. So what does {var} mean?! Confused

I wouldn't change it

bye,
André
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP