in reply to using template on same page

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;

Replies are listed 'Best First'.
Re^2: using template on same page
by bigup401 (Pilgrim) on Sep 15, 2018 at 14:48 UTC

    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

Re^2: using template on same page
by bigup401 (Pilgrim) on Sep 15, 2018 at 16:45 UTC

    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

        i dont get u well. but am trying to get poll foo,bar,moo from db

      $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

        sorry it was wrong writing but its. i mistake @names with @$names. but thats the code. i dont know failed to pull from db just outputing

        ARRAY(0x2190674) ARRAY(0x2190674) ARRAY(0x2190684)
        $template->process (\$templ, { list => \@$names }) or die $template->error;
        </code>