Ok, I'll try my best to give an accurate working example.   Our application 
deals with tracking information about computers installed at various 
sites.  One site may have many computers.  We need to give the users the 
ability to edit all of the info about all of the computers at a particular 
site all at once in one form.  So a form might look something like:

<form...>
<input type=text name=computer_1_name>
<input type=text name=computer_1_ip_address>
<input type=text name=computer_2_name>
<input type=text name=computer_2_ip_address>
<input type=submit>

This way, they can edit both computers at this site at once and submit the 
changes.  So far, so good.  But this is hard-coded and doesn't scale at all 
for when we have 3 or 4 or 40 computers.  So, we want to HTML::Template-ize 
this.  So you might make a file called edit_computers.tmpl like this:

<form...>
<tmpl_loop computers>
<input type=text name=computer_<tmpl_var computer_id>_name>
<input type=text name=computer_<tmpl_var computer_id>_ip_address>
</tmpl_loop>
<input type=submit>

Looks good.  However, you want to do some error checking when they submit 
the form, say to check that the ip address they entered doesn't match the 
one for another computer at the same site.  So, you write some server-side 
code to check for that in the database, and if it finds a duplicate, you 
want to alert the user and have him enter some other ip address.  The nice 
thing to do is to show him the same form he had just filled in with the 
values he previously entered, with maybe some red text that says "Sorry, 
the IP address for Computer 2 is already assigned to another computer at 
this site."  So I think, "hey, HTML::Template's associate feature comes in 
handy, I'll create a CGI object of the form, and hand that to the template 
to associate, so the form can fill itself back in and the user doesn't have 
to retype everything."  You'll recall that associate simply takes every 
param() from the CGI and "publishes" it back to the template as 
tmpl_var's.  So, in my example, I could do this:

...
my $cgi = new CGI;
my @params = $cgi->param();
my @errors;
for my $param (@params) {
   if $param =~ /_ip_address/ and alreadyExistsInDb($cgi->param($param)) {
     push @errors, {error_text => "The IP address $cgi->param($param) is 
already assigned."};
   }
}
if (length @errors > 0) {
   my $tmpl = new HTML::Template(filename => 'edit_computers.tmpl', 
associate => $cgi);
   $tmpl->param(errors => \@errors);
   print $cgi->header . $tmpl->output;
   return
}
# else go about your business

So now you've managed in my example to set computer_1_name, 
computer_1_ip_address, computer_2_name, and computer_2_ip_address in your 
template.  So far so good.  The last thing remaining is to edit the 
template to fill in those form fields for you.  So here you go:

<tmpl_loop errors>
   <tmpl_loop error_text><br>
</tmpl_loop>
<form...>
<tmpl_loop computers>
<input type=text name=computer_<tmpl_var computer_id>_name  value=<tmpl_var 
OOPS_CANT_DO_IT>>
<input type=text name=computer_<tmpl_var 
computer_id>_ip_address  value=<tmpl_var OOPS_CANT_DO_IT>>
</tmpl_loop>
<input type=submit>

There's no way to get that value to be value=<tmpl_var computer_1_name> on 
the first pass and value=<tmpl_var computer_2_name> on the second, because 
that would require this:

<input type=text name=computer_<tmpl_var computer_id>_name  value=<tmpl_var 
computer_<tmpl_var computer_id>_name>>

which doesn't work.  Does everyone see my dilemma now?  Thanks for being 
patient! =)

-Fran



At 04:06 PM 8/13/2002 -0700, Brian McCain wrote:

>Without knowing how you're using the associate feature, it's hard to come up
>with a workaround. It seems like loops should work for you, but if whatever
>youre doing with associate is making that impossible, perhaps you could
>include the portion of your code where you use associate?
>
>Brian
>
>----- Original Message -----
>From: "Fran Fabrizio" <[EMAIL PROTECTED]>
>To: "Cory Trese" <[EMAIL PROTECTED]>
>Cc: <[EMAIL PROTECTED]>; "HTML::Template List"
>Sent: Tuesday, August 13, 2002 3:24 PM Subject: RE: [htmltmpl] Dynamic 
>TMPL_VAR names? Is it possible? | | A loop does me no good. If I have two 
>sites, and I use a loop like | suggested, I get a form that looks 
>something like: | | | | | etc.... | | Two form fields in the same form 
>with the same name = broken form. | :-( Even worse, when I go to 
>repopulate the 'associate' feature | has I think now clobbered one of the 
>site_name values and both of | these form fields will get the same data in 
>the value= attribute. | :-( In general, yes, loops are the answer, but 
>when building forms | and/or trying to repopulate forms using the 
>'associate' feature of | H:T, loops don't work. | | I appreciate the quick 
>response though! Any other ideas? | | -Fran | | >You need a loop. See the 
>HTML::Template docs on LOOPS. | > | > | > | >
>| > | > | > | > | >You pass a loop (AKA, "sites => 
>\@sites_array_of_hashref," ) with the | >necessary sites. This loop 
>construction is business-logic (we would | use a | >module that prepared 
>an array of hashrefs from a DBI routine.) | | | | | | | | | | | 
>------------------------------------------------------- | This sf.net 
>email is sponsored by: Dice - The leading online job board | for high-tech 
>professionals. Search and apply for tech jobs today! | 
>http://seeker.dice.com/seeker.epl?rel_code=31 | 
>_______________________________________________ | Html-template-users 
>mailing list | [EMAIL PROTECTED] | 
>https://lists.sourceforge.net/lists/listinfo/html-template-users | --- 
>Incoming mail is certified Virus Free. Checked by AVG anti-virus system 
>(http://www.grisoft.com). Version: 6.0.381 / Virus Database: 214 - Release 
>Date: 8/2/2002

Reply via email to