Hey,
 
 I am coming in at the middle of this conversation but I think I have a 
 better way.  I think those crazy perl one liners cause to much 
 confusion when some else tries to update/change your code.  I use 
 fetchrow_hashref() to do the same thing in a snippet below but I have 
 room to add things to the loop (i.e. a cgi->param()) and I think it is 
 more readable.
 
 Here is a code example:
 
     # Get the data from MySQL using a DBI Handle
     my $sth = $dbh->prepare("
        SELECT 
           Thing.value as Thing,              # Thing must be a 
 tmpl_var in the loop          OtherThing.value as OtherThing     # 
 Same here        FROM           Thing, OtherThing        WHERE         
   They have a relationship");    $sth->execute() or die DBI::errstr;
 
     # open the html template
     my $template = HTML::Template->new(filename => 'test.tmpl');
 
     # Build the tmpl_loop data structure and load the template
     while (my $data = $sth->fetchrow_hashref())
     {
         # Manipulate a single thing in the loop that 
         # was not in the query results
         $$domain{'AnotherThing'} = $cgi->param('AnotherThing');
 
         # Put the rest of the stuff from the query in the loop 
         push (@loopData, $data);
     }
     # Give the data to the template
     $template->param('MyLoop' => \@loopData);
 
 Sincerely,
 Kenny Pyatt
 President
 Venzia
 http://www.venzia.com
 
> ---------- Original Message -----------
> From: Jim Lang <[EMAIL PROTECTED]>
> To: [EMAIL PROTECTED]
> Sent: Thu, 26 Sep 2002 16:04:47 -0600
> Subject: Re: [htmltmpl] HTML-Template and DBI
> 
> > Doc I think I understand your confusion...
> > 
> > Without doubt it was the PerlMonks tutorial at
> > http://perlmonks.thepen.com/65642.html
> > 
> > that actually allowed me to use DBI and HTML::Template
> > together.
> > 
> > But I still had a problem with this part:
> > 
> > # prepare a data structure for HTML::Template
> > my $rows;
> > push @{$rows}, $_ while $_ = $sth->fetchrow_hashref();
> >  
> > # instantiate the template and substitute the values
> > my $template = HTML::Template->new(filename => 'songs.tmpl');
> > $template->param(ROWS => $rows);
> > 
> > Yes, it got the data out and put it into the template,  but
> > I didn't like the fact that I didnt have a value, lets say $title,
> > to use.  See, I was used to doing a select, and then a while loop
> > in which I would output html, so I could just write html freely,
> > and have $column_name variables for the data output.  This is pretty
> > much what you do inside the tmpl_loop.
> > 
> > But you only see that in the template file. Again from the
> > PerlMonks tutorial at http://perlmonks.thepen.com/65642.html
> > 
> > <!-- songs.tmpl -->
> > <html>
> > <head>
> > <title>Song Listing</title>
> > </head>
> > <body>
> > <h1>My Songs</h1>
> > <table>
> > <!-- TMPL_LOOP NAME=ROWS -->
> >    <tr>
> >       <td><!-- TMPL_VAR NAME=TITLE --></td>
> >       <td><!-- TMPL_VAR NAME=ARTIST --></td>
> >       <td><!-- TMPL_VAR NAME=ALBUM --></td>
> >       <td><!-- TMPL_VAR NAME=YEAR --></td>
> >    </tr>
> > <!-- /TMPL_LOOP -->
> > </table>
> > </body>
> > </html>
> > 
> > and there are your column names.  
> > 
> > But I still find this awkward sometimes when I would prefer to manipulate
> > the data prior to spitting it out to the template.  I find myself resorting
> > to javascript when faced with this... I'm sure there is a better way.
> > 
> > Well hopefully I've added something to the discussion besides more 
> > confusion..
> > 
> > Jim
> > 
> > -------------------------------------------------------
> > This sf.net email is sponsored by:ThinkGeek
> > Welcome to geek heaven.
> > http://thinkgeek.com/sf
> > _______________________________________________
> > Html-template-users mailing list
> > [EMAIL PROTECTED]
> > https://lists.sourceforge.net/lists/listinfo/html-template-users
> ------- End of Original Message -------
------- End of Original Message -------



-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
Html-template-users mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/html-template-users

Reply via email to