in reply to HTML::Template, DBI and <TMPL_LOOP>'s

i have worked with dkubb and here is how i interpret and boil it down:
my $dbh =DBI->connect( "dbi:$dbidriver(RaiseError=>1,Taint=>1):dbname=$dbname;host=$d +bhost", "$dbusername", "$dbpassword", ); my $statement= "select name from names"; my $sth = $dbh->prepare($statement); $sth->execute(); ##grab all rows as array ref my $site = $sth->fetchall_arrayref; $sth->finish; $dbh->disconnect; my %stuff; ##push everything into a hash foreach my $site_in ( @{$site} ) { push @{ $stuff{view} }, { name => $site_in->[0], }; } #load file my $template = HTML::Template->new( filename => catfile( foo.tmpl ), die_on_bad_params => 0 ); #load in hash $template->param( {%stuff} ); #print out print $template->output;
this is foo.tmpl:
<table> <tr><td>name</td></tr> <tmpl_loop name=view> <tr> <td><tmpl_var name=name></td> </tr> </tmpl_loop>
sorry if anything catches n breaks, i had to pull a lot of this out of some subroutines i used to break up the workload

Replies are listed 'Best First'.
Re: Re: HTML::Template, DBI and <TMPL_LOOP>'s
by jdtoronto (Prior) on Jan 03, 2004 at 02:46 UTC
    Hi drfrog,

    The point is that their is no need to build the list of hashes! You can get it straight from the database. By changing:

    # This method returns a reference to an array of array-refs my $site = $sth->fetchall_arrayref;
    to:
    # This method returns a reference to an array of hash-refs my $site = $sth->fetchall_arrayref( {} );
    Produces a structure which can be directly passed to the TMPL_LOOP, like this:
    $template->param( view => $site );
    assuming of course that the one field returned, using your example code, was name.

    My reference to dkubb was not to in any way impune what he did, but merely to try and cite the appropriate references as I knew them. The method he used originally was removed and this one obviously added in its place!

    jdtoronto

      doh!
      next time i should try and read the whole thread, and then reply! answering 10 minutes before leaving work didnt help either!

      thanks for asking this question, i had a bit of disconnect on this loading hashes straight from the database too!

      btw:
      i didnt take your references to dkubb as anything negative :)