So I'm not new to perl, but I've not used Template::Toolkit before. Yes, for years I have been mixing my code and my html output with print statements. I know, I know, but I'm trying to do the right thing now and move to Template. I'm having difficulty in getting my FOREACH in the template to output my dataset. I must be missing something but I just don't see where my mistake is. If anyone would look this over and provide suggestions it would be greatly appreciated.

My template (sandbox.tt2) code is:

<!DOCTYPE html> <html> <head> <title>Sandbox</title> </head> <body> <ul> [% FOREACH row in rows %] <li>[% row.ams_wac %] - [% row.sitename %]</li> [% END %] </ul> </body> </html>

and my perl code is:

#!/usr/bin/perl -w use strict; use warnings; use v5.26; use CGI; use Template; use DBI; use Data::Dumper; use lib qw(.); require "utils.pl"; my $content = "Content-type: text/html\n\n"; my $template = ''; my $vars = ''; my $conn = ''; my $rows = ''; $conn = dbconnect(); $rows = $conn->selectall_arrayref("select ams_wac, sitename from sites + where active = 'TRUE' order by sitename",{Slice => {}}); $template = Template->new({ INCLUDE_PATH => ['templates'] }); print Dumper($rows); $template->process('sandbox.tt2', {rows => $rows}, \$content) or die $template->error(); dbdisconnect($conn); print $content;

My output is not looping through the dataset:

Content-type: text/html <!DOCTYPE html> <html> <head> <title>Sandbox</title> </head> <body> <ul> </ul> </body> </html>

and Dumper shows I do have data in $rows:

$VAR1 = [ { 'sitename' => 'Site One', 'ams_wac' => 'SLRHC' }, { 'ams_wac' => 'SLR', 'sitename' => 'Site Two' }, { 'sitename' => 'Site Three', 'ams_wac' => 'SLRSIX' } ];

I've got to be missing something simple and I'm sure the answer will reinforce the fact that I'm an idiot. Thanks for any help.


In reply to Template Toolkit troubles with looping through database resultset by mmceldowney

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.