Have you considered using Rose::HTML::Objects? Here's a minimal, quick solution to your specific issue:
$field =
Rose::HTML::Form::Field::SelectBox->new(
name => 'whatever',
multiple => 1,
options => [ 1 => 'One', 2 => 'Two', 3 => 'Three' ],
size => 3);
$field->input_value([ 1, 3 ]);
print $field->html;
or in TT-speak:
[% field.html %]
both of which produce:
<select multiple name="whatever" size="3">
<option selected value="1">1</option>
<option value="One">One</option>
<option value="2">2</option>
<option value="Two">Two</option>
<option selected value="3">3</option>
<option value="Three">Three</option>
</select>
You can manage your whole form using Rose::HTM::Objects as well, of course:
$form = Rose::HTML::Form->new;
$form->add_fields
(
whatever =>
{
type => 'select box',
multiple => 1,
options => [ 1 => 'One', 2 => 'Two', 3 => 'Three' ],
size => 3,
},
...
);
# Pass in form params from various sources:
# Hashref
$form->params({ whatever => [ 1, 3 ] });
# CGI object
$form->params_from_cgi($cgi); # $cgi "isa" CGI
# Apache request object (mod_perl 1 or 2)
$form->params_from_apache($r);
# Initialize the fields based on params
$form->init_fields();
Then, finally:
print $form->start_html,
$form->field('whatever')->html,
...
$form->end_html;
or in TT:
[% form.start_html %]
[% form.field('whatever').html %]
...
[% form.end_html %]
(For XHTML output, just replace the "html" method calls with "xhtml")
Yeah, I'm sure it looks "heavyweight", but life's too short to be manually producing form widget markup using TT, or any other templating language, IMO :)