Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is there anyway to print sub routine in HTML::TEMPLATE?

like this

sub print(){
print "hello worl";
print "please let this work";
}

What im trying to do is have a sub routine connect to a database take the information and make an html table with it and print the table out. I want to use html::template so I can completely separate the html from the perl code so my design team can do there magic without them interupting my progress.

Thanks,
Cory

20031029 Edit by jeffa: Changed title from 'CGI HTML::TEMPLATES '

  • Comment on Display Database table with CGI and HTML::Template

Replies are listed 'Best First'.
Re: Display Database table with CGI and HTML::Template
by BUU (Prior) on Oct 29, 2003 at 08:38 UTC
    Dear god. Did you read the docs at all? Don't do what you think you want to do. Do this:
    my $ht = new HTML::Template(filename=>'main.tmpl'); my $db_data=print(); $ht->params(db_data=>$db_data,%stuff); print $ht->output; sub print() { my $ht = new HTML::Template(filename=>'database_info.tmpl'); @foo=$dbh->do("select * from *"); # ok so it's not even vaugely lega +l. do yer own db code $ht->params(dbstuff=>@foo); return $ht->output; #probably not optimal }
    In other words, don't do html in the sub, just have the sub initialize another template and then return the template output or the template object.

      I think your example is too complicated. It made me look twice, so anyone unfamiliar with HTML::Template will likely not understand it at all. You're actually using a template and inserting its output into another template. Pretty confusing.

      A simpler method would be to use a TMPL_LOOP for the table of data entries, and put the actual data in a suitable data structure for HTML::Template to make a nice table in the TMPL_LOOP.

      On second thought, that might not be much simpler at all, since it requires understanding of complicated data structures.

      --

      Morbo: "Windmills do not work that way!"

Re: Display Database table with CGI and HTML::Template
by jeffa (Bishop) on Oct 29, 2003 at 13:00 UTC
Re: Display Database table with CGI and HTML::Template
by samtregar (Abbot) on Oct 29, 2003 at 23:15 UTC