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

my script works ok, but when running it outputs in plain text

wat i want is to output on same cgi page without calling external html file to be processed by template module

its like i dont want template to process html file from other source. i want it to process the html code which is below

#THIS IS THE TEST.PL use strict; use warnings; use Template; my @names = qw/ Foo Bar Moo /; my $template = Template->new; $template->process('test.pl', { list => \@names }) or die $template->error; print <<START_HTML; <!DOCTYPE html"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +" /> <title>Untitled Document</title> </head> <body> [% FOREACH name IN list %] <li> <div class='inforno'> <img src='inforno'> </div> <div class='inforno'> <a href='#' class='inforno'>[% name %]</a> </div> <span class='inforno'> <a href='#' class='inforno'>Edit user</a> </span> </li> [% END %] </body> </html>

OUTPUT IN BROWSER

use strict; use warnings; use Template; my @names = qw/ Foo Bar Moo /; my $template = Template->new; $template->process('test.pl', { list => \@names }) or die $template->error; print <<START_HTML; <!DOCTYPE html"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +" /> <title>Untitled Document</title> </head> <body> <li> <div class='inforno'> <img src='inforno'> </div> <div class='inforno'> <a href='#' class='inforno'>Foo</a> </div> <span class='inforno'> <a href='#' class='inforno'>Edit user</a> </span> </li> <li> <div class='inforno'> <img src='inforno'> </div> <div class='inforno'> <a href='#' class='inforno'>Bar</a> </div> <span class='inforno'> <a href='#' class='inforno'>Edit user</a> </span> </li> <li> <div class='inforno'> <img src='inforno'> </div> <div class='inforno'> <a href='#' class='inforno'>Moo</a> </div> <span class='inforno'> <a href='#' class='inforno'>Edit user</a> </span> </li> </body> </html> START_HTML <!DOCTYPE html"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +" /> <title>Untitled Document</title> </head> <body> [% FOREACH name IN list %] <li> <div class='inforno'> <img src='inforno'> </div> <div class='inforno'> <a href='#' class='inforno'>[% name %]</a> </div> <span class='inforno'> <a href='#' class='inforno'>Edit user</a> </span> </li> [% END %] </body> </html>

Replies are listed 'Best First'.
Re: using template on same page
by hippo (Archbishop) on Sep 15, 2018 at 10:39 UTC

    Use a scalar ref with the template contents as described in the Template documentation.

    #THIS IS THE TEST.PL use strict; use warnings; use Template; my @names = qw/ Foo Bar Moo /; my $template = Template->new; my $templ = <<START_HTML; <!DOCTYPE html"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +" /> <title>Untitled Document</title> </head> <body> [% FOREACH name IN list %] <li> <div class='inforno'> <img src='inforno'> </div> <div class='inforno'> <a href='#' class='inforno'>[% name %]</a> </div> <span class='inforno'> <a href='#' class='inforno'>Edit user</a> </span> </li> [% END %] </body> </html> START_HTML $template->process (\$templ, { list => \@names }) or die $template->error;

      thanks

        Just wanted to say, I'm really glad you chose to use a templating system. Template::Toolkit is one of the most common hammers for this type of nail.


        Dave

      tho i get this error when i try to get array from db. but the code works ok just only not getting array's

      #MY SCRIPT use strict; use warnings; use Template; my $template = Template->new; if($info) { my $select = $DBH->prepare("SELECT FOO, BAR, MOO FROM tble WHERE CONCA +T(FOO, ', ', BAR, ', ', MOO) LIKE ?"); $select->execute('%'.$info.'%'); $names = $select->fetchall_arrayref(); foreach $names ( @$names) { ($variable1, $variable2, $variable3) = @$names; } } my $templ = <<START_HTML; <!DOCTYPE html"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +" /> <title>Untitled Document</title> </head> <body> [% FOREACH name IN list %] <li> <div class='inforno'> <img src='inforno'> </div> <div class='inforno'> <a href='#' class='inforno'>[% name %]</a> </div> <span class='inforno'> <a href='#' class='inforno'>Edit user</a> </span> </li> [% END %] </body> </html> START_HTML $template->process (\$templ, { list => \@names }) or die $template->error;

      MY OUTPUT

      ARRAY(0x2030674) ARRAY(0x2030634) ARRAY(0x2030618)

        You may not have noticed this yet but the template you posted generates <li>...</li> pairs, but never creates an encompassing <ul>...</ul> nor <ol>...</ol> construct to contain the list items.


        Dave

        Your script as posted doesn't compile for me. I get the following errors when running it:

        Global symbol "$info" requires explicit package name (did you forget t +o declare "my $info"?) at tmp.pl line 9. Global symbol "$DBH" requires explicit package name (did you forget to + declare "my $DBH"?) at tmp.pl line 11. Global symbol "$info" requires explicit package name (did you forget t +o declare "my $info"?) at tmp.pl line 12. Global symbol "$names" requires explicit package name (did you forget +to declare "my $names"?) at tmp.pl line 13. Global symbol "$names" requires explicit package name (did you forget +to declare "my $names"?) at tmp.pl line 14. Global symbol "$names" requires explicit package name (did you forget +to declare "my $names"?) at tmp.pl line 14. Global symbol "$variable1" requires explicit package name (did you for +get to declare "my $variable1"?) at tmp.pl line 15. Global symbol "$variable2" requires explicit package name (did you for +get to declare "my $variable2"?) at tmp.pl line 15. Global symbol "$variable3" requires explicit package name (did you for +get to declare "my $variable3"?) at tmp.pl line 15. Global symbol "$names" requires explicit package name (did you forget +to declare "my $names"?) at tmp.pl line 15. Global symbol "@names" requires explicit package name (did you forget +to declare "my @names"?) at tmp.pl line 48. tmp.pl had compilation errors.

        Can you please post the code you're actually running?

        Also, if you're using strict.pm, it allows Perl to tell you when you forget to declare a variable. Going through your code and thinking about where you first use a variable will make you find typos in your script where you mistype a variable name, or mistype a variable type.

        Which of the fields FOO, BAR, MOO contains the names ?

        Also, <!DOCTYPE html"> should not have the double quote in it.

        poj
        $names = $select->fetchall_arrayref(); foreach $names ( @$names) { ($variable1, $variable2, $variable3) = @$names; }

        and then

        $template->process (\$templ, { list => \@names }) or die $template->error;

        I bet this last names you wanted it to be the other names and not the names perl has in mind.

        names, names, all these names!!! fix these (and what others told you) and your program will be happier: re the joy of anonymity...

        bw, a horse with no name