Text::Template Question

Discussion in 'Perl Misc' started by webvoip, Mar 8, 2005.

  1. webvoip

    webvoip Guest

    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
     
    webvoip, Mar 8, 2005
    #1
    1. Advertising

  2. webvoip

    Jim Keenan Guest

    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
     
    Jim Keenan, Mar 8, 2005
    #2
    1. Advertising

  3. webvoip

    webvoip Guest

    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
     
    webvoip, Mar 8, 2005
    #3
  4. * 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-Template-1.44/lib/Text/Template.pm

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

    regards,
    fabian
     
    Fabian Pilkowski, Mar 8, 2005
    #4
  5. webvoip

    webvoip Guest

    thanks fabian.

    i did read the docs. i guess i didnt grasp the $OUT trick as well as
    you did.
     
    webvoip, Mar 8, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Theis
    Replies:
    2
    Views:
    500
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    566
    tom_usenet
    Jul 24, 2003
  3. Replies:
    1
    Views:
    2,147
    Gianni Mariani
    Jun 8, 2007
  4. Peng Yu
    Replies:
    3
    Views:
    805
    Thomas J. Gritzan
    Oct 26, 2008
  5. nguillot
    Replies:
    5
    Views:
    558
Loading...

Share This Page