Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Text::Template Question

Reply
Thread Tools

Text::Template Question

 
 
webvoip
Guest
Posts: n/a
 
      03-08-2005
EHLO,

I'm toying around with Text::Template and have a question for anyone
who may know this out there...

I have a template that has the following code:

<table>
<tr><td colspan="100%">Edwin's Test Table</td></tr>
{
my $sth = $dbh->prepare("select * from t");

$sth->execute;

while (my $h = $sth->fetchrow_hashref)
{
print " <tr><td>$h->{id}</td><td>$h->{stuff}</td></tr>\n";
}
}
</table>

And my calling application exports the $dbh handle via the package
option using fill_in:

$PackageName::dbh =
DBI->connect("DBI:mysql:database=test;host=test1", "username",
"password") || die($!);;

my $t = Text::Template->new(TYPE => 'FILE', SOURCE =>
'../templ/t.tmpl') or die ($Text::Template::Error);

my $text = $t->fill_in(PACKAGE => 'PackageName');

print $text;

It works... somewhat... The problem i am having is that the print order
is wrong. Is there some pre-processing or other simple option i am
missing?

Here's what prints out (notice the table rows print first):

<tr><td>1</td><td>one</td></tr>
<tr><td>2</td><td>two</td></tr>
<tr><td>3</td><td>three</td></tr>
<tr><td>4</td><td>four</td></tr>
<tr><td>5</td><td>five</td></tr>
<html>
<body bgcolor="#ffffff" text="#000000">

<table>
<tr><td colspan="100%">Edwin's Test Table</td></tr>
<!-- table rows should be here -->
</table>
</body>
</html>

Thanks and apologies if i posted this to the wrong newsgroup.

-Edwin

 
Reply With Quote
 
 
 
 
Jim Keenan
Guest
Posts: n/a
 
      03-08-2005
webvoip wrote:
> EHLO,
>
> I'm toying around with Text::Template and have a question for anyone
> who may know this out there...
>
> I have a template that has the following code:
>
> <table>
> <tr><td colspan="100%">Edwin's Test Table</td></tr>
> {
> my $sth = $dbh->prepare("select * from t");
>
> $sth->execute;
>
> while (my $h = $sth->fetchrow_hashref)
> {
> print " <tr><td>$h->{id}</td><td>$h->{stuff}</td></tr>\n";
> }
> }
> </table>
>


A preliminary question: Have you verified that, independently of
Text::Template, your program prints what you expect as of this point?
(I.e., let's make sure we have clearly sorted out DBI problems from
Text::Template problems.)

Jim Keenan

 
Reply With Quote
 
 
 
 
webvoip
Guest
Posts: n/a
 
      03-08-2005
Hi Jim,

The values in the table rows do correctly mirror the data in the
database table. I am of the opinion DBI is doing its job correctly. I
think the problem lies in my Text::Template implementation.

Reading the docs, I see Text::Template scans in the template first, and
interpolates any perl code. I am guessing it is during this phase that
it is expanding my print statements and this is what is causing them to
print before the calling application calls: "print $text";

i guess the table rows are not stored in the $text variable because
they are just print statements in the template.

?,

Edwin

 
Reply With Quote
 
Fabian Pilkowski
Guest
Posts: n/a
 
      03-08-2005
* webvoip wrote:
>
> I'm toying around with Text::Template and have a question for anyone
> who may know this out there...
>
> I have a template that has the following code:
>
> <table>
> <tr><td colspan="100%">Edwin's Test Table</td></tr>
> {
> my $sth = $dbh->prepare("select * from t");
> $sth->execute;
> while (my $h = $sth->fetchrow_hashref) {
> print " <tr><td>$h->{id}</td><td>$h->{stuff}</td></tr>\n";
> }
> }
> </table>


Please read this modules docs before asking here. Text::Template will
print the returning value of each code block. As described, you can also
try to use the special variable called $OUT (this is what i would do).

{
my $sth = $dbh->prepare("select * from t");
$sth->execute;
while ( my $h = $sth->fetchrow_hashref ) {
$OUT .= "<tr><td>$h->{id}</td><td>$h->{stuff}</td></tr>\n";
}
}

Please read the docs e.g. at

http://search.cpan.org/~mjd/Text-Tem...xt/Template.pm

and learn why not using prints in your template's code blocks.

regards,
fabian
 
Reply With Quote
 
webvoip
Guest
Posts: n/a
 
      03-08-2005
thanks fabian.

i did read the docs. i guess i didnt grasp the $OUT trick as well as
you did.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments