...

View Full Version : HTML::Templates (checkboxes)



tdavis
09-22-2006, 01:57 AM
I cant find anything that tells me how to set up a template variable for checkboxes and/or radio buttons. Can it be done?

Here is a text field using HTML::Templates
value=<TMPL_VAR NAME="myfieldname">

Is it possible to use HTML::Templates to set one or more of the following checkboxes to "checked"? I guess the same type of thing, but of course only one selected, with radio button groups?

<input type="checkbox1" name="chk1" value="1" checked>
<input type="checkbox2" name="chk2" value="2">
<input type="checkbox3" name="chk3" value="3">

Thanks,
-tdavis

FishMonger
09-22-2006, 03:54 AM
Here's an example taken from one of my scripts.


<TMPL_LOOP name="CATEGORIES">
<input type="radio" class="aligncenter" id="category" name="category" value="<tmpl_var name="VALUE">" <tmpl_var name="CHECKED"> /><tmpl_var name="VALUE">
</TMPL_LOOP>

my @category = create_category_array();

$template->param(CATEGORIES => \@category);

sub create_category_array {

my @categories = qw(OSH NETWORKING SUPPORT);
my @category;

for (@categories) {
my %category;
$category{'VALUE'} = $_;
$category{'CHECKED'} = 'checked="checked"' if exists $form{'category'} && $form{'category'} eq $_;
push @category, \%category;
}
return @category;
}

tdavis
09-22-2006, 11:56 AM
In your HTML example, you dont have mutlitple radio buttons defined, you have a TMPL_LOOP that is loaded up in the Perl script? I have not found that in any of the HTML::Template stuff I have found. Thanks!

What about checkboxes?
Are they handled the same way?

In my example, I guess I made a mistake. They are all type=checkbox, but have different names because more than one can be selected.

FishMonger
09-22-2006, 01:55 PM
In your HTML example, you dont have mutlitple radio buttons defined, you have a TMPL_LOOP that is loaded up in the Perl script? I have not found that in any of the HTML::Template stuff I have found. Thanks!

What about checkboxes?
Are they handled the same way?

In my example, I guess I made a mistake. They are all type=checkbox, but have different names because more than one can be selected.

TMPL_LOOP expects to be passsed (by reference) an array-of-hashes.
Here's the module's documentation. http://search.cpan.org/~samtregar/HTML-Template-2.8/Template.pm#TMPL_LOOP

The only (key) difference between my example and the module's is the method used to build/declare the array reference.

My example is using radio buttons, but checkboxes are handled in the same way.

tdavis
09-22-2006, 05:24 PM
OK. So my checkbox value (example below) will not work? I have to use a TMPL_LOOP? It does not appear to work this way. If I set the value of the CHECK to "yes" I would expect it to be checked, if it is blank then not checked. But, it is checked either way when I tried this.

<input type="checkbox" name="field" checked="<TMPL_VAR NAME="check">">

FishMonger
09-22-2006, 05:41 PM
You need to put 'checked=' as part of the template param and offcially, the value of the checked attribute should be 'checked' not 'yes'.

$template->param(check => 'checked="checked"');

<input type="checkbox" name="field" <TMPL_VAR NAME="check">>

FishMonger
09-22-2006, 05:50 PM
I think this is obvious, but if you dont want anything checked, you simply don't assign a value to the param i.e. it should have a value of undef or an empty string.

tdavis
09-22-2006, 06:08 PM
Thanks. I got it to work using <input type="checkbox" name="field" <TMPL_VAR NAME="check">> where the value of the template variable "check" is either blank or "checked". I hope is the correct way to do it. But I am not sure what you mean by $template->param(check => 'checked="checked"'); I did like this: $template->param(checkbox => $checked);. It works anyway.

Now onto drop down boxes. I bet they work the same way, but using "selected" as a variable name like this: <option value="Sports" selected>SPORTS Price Group</option>. Does that look right?

tdavis
09-22-2006, 06:09 PM
Oops. Like this with the variable:

<option value="Sports" <TMPL_VAR NAME="choice4">>SPORTS Price Group</option>

tdavis
09-22-2006, 07:19 PM
The drop down box worked too!

Thanks again FishMonger!
You have been such a great help! :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum