html_object_options
| From: | Marcus Bointon | Date: | Wed, 28 Jan 2004 18:51:54 +0000 |
| Subject: | html_object_options | ||
| Groups: | php.smarty.dev | ||
| Request: | Send a blank email to php-smarty-dev+get-2075@lists.php.net to get a copy of this message | ||
I've written a plugin based on the regular html_options one that allows you
to pass an array of objects as the list of items to build the select for.
You specify which properties of the objects should be used for values and
output and you can provide an array of items to be pre-selected which need
to match the submitted value properties.
E.g.
In otest.php:
<?php
class otest {
var $id;
var $name;
}
$a =& new otest;
$b =& new otest;
$a->id = 1;
$a->name = 'a';
$b->id = 2;
$b->name = 'b';
$ar = array($a, $b);
$sel = 1;
require_once 'Smarty.class.php';
$s =& new Smarty;
$s->assign('ar', $ar);
$s->assign('sel', $sel);
$s->display('otest.tpl');
?>
And in otest.tpl:
{html_object_options objects=$ar outputprop="name" valueprop="id"
selected=$sel}
This results in this being output:
<select name="select">
<option label="a" value="1" selected="selected">a</option>
<option label="b" value="2">b</option>
</select>
The only problem I can see with this is that the object property names
appear in the template, which is not good for separation, however, it's very
useful apart from that, and I don't see that it's worse than having array
key names in there.
If you think this is a useful extension, I'm happy to post it here or to
someone that can commit it.
Other comments welcome too!
Marcus
--
Marcus Bointon
Synchromedia Limited: Putting you in the picture
marcus@synchromedia.co.uk | http://www.synchromedia.co.uk