----- Original Message -----
From: "Aranya Ghatak" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, April 04, 2002 2:13 AM
Subject: Re: [htmltmpl] How to print SELECTED in an OPTION
> That looks like hard coding it only for the third option. How is it ever
> going to select Item One, or Item Two, if those were ever passed as
values?
> (I've tried putting the <tmpl_if> on all of the lines together, but that
> doesn't work).
> I've wrestled with this for hours.
> Forget multiple values, here is a simpler scenario I have been struggling
> with: how would I pass a Boolean value and show a Yes/No Radiobutton
> selected properly?
Flemming's response is only hardwired on the Perl side - there are several
ways to populate the array-of-hashrefs or to iterate through an existing
array to dynamically add the 'selected' or 'checked' value. The template
and code below should do what you want....
<!-- select.htt -->
<html>
<head>
<title>Using TMPL_LOOP to Populate a SELECT Tag</title>
</head>
<body>
<h1>Using TMPL_LOOP to Populate a SELECT Tag</h1>
<form name="myform">
<p>Select an option<br>
<select name="select_options">
<TMPL_LOOP select_data>
<option value="<TMPL_VAR value>" <TMPL_VAR selected>><TMPL_VAR
label></option>
</TMPL_LOOP>
</select>
</p>
<p>Select another option<br>
<TMPL_LOOP radio_data>
<input name="radio_options" type="radio" value="<TMPL_VAR value>"
<TMPL_VAR checked>><TMPL_VAR label><br>
</TMPL_LOOP>
</p>
</form>
</body>
</html>
#
#htmltmpl_select.pl
#
use strict;
use HTML::Template;
my $template = new HTML::Template(filename => 'select.htt');
# Populate the select box with values and labels
my @select_data = (
{ value => 1, label => 'Item One' },
{ value => 2, label => 'Item Two' },
{ value => 3, label => 'Item Three' },
);
# And now the radio buttons
my @radio_data = (
{ value => 1, label => 'Item One' },
{ value => 2, label => 'Item Two' },
{ value => 3, label => 'Item Three' },
);
# Iterate through @select_data to choose the selected value.
# This is hardwired as an example; a real application would
# fetch the value from somewhere like $cgi->param('select_options').
my $select_value = 2;
foreach my $item (@select_data) {
$item->{selected} = 'selected' if $item->{value} == $select_value;
}
# The same goes for the radio buttons
my $radio_value = 3;
foreach my $item (@radio_data) {
$item->{checked} = 'checked' if $item->{value} == $radio_value;
}
# Fill in and print the template
$template->param(
select_data => \@select_data,
radio_data => \@radio_data
);
print $template->output();
HTH,
Kurt Stephens
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]