Text::Template Question

W

webvoip

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
 
J

Jim Keenan

webvoip said:
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
 
W

webvoip

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
 
F

Fabian Pilkowski

* webvoip said:
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-Template-1.44/lib/Text/Template.pm

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

regards,
fabian
 
W

webvoip

thanks fabian.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top