I hope somebody has replied already.
If not, in general you are reading data from DATA,
creating an array and a hash, and then creating an
html file using the Template Toolkit.

I think much of it looks good, but I see no
use Template::Toolkit
or anything similar.
Do you have that?  I suspect you do.
Maybe you should post that part too.


Also, this line looks suspicious to me:
my %list = (list => \@courses);

Maybe that is intended to be an array ref.

Perhaps right after that you should put this:

print "\%list contains this:\n\n";
foreach my $key (sort keys %list){
    print "$key - $list{$key}\n";
}

__END__



This is a way for you to debug it yourself.
If that works as expected, then get back to us.


Mike


On 10/28/2018 3:45 PM, Rick T wrote:
As a novice in perl I realize that it’s a bit presumptuous for me to attempt references and complex data structures. But I had a need and gave it a shot — a failing shot. I’ve been fiddling with my failure, almost mindlessly, all weekend; now I need some help.

Below is the template segment I am trying to populate with data, and following it is the segment of code that attempts to call it. The output I get in my browser is persistently empty, with every instance of [% %] being replaced with banks.


start HTML ------------
<table>
[% FOREACH course IN courses %]
<br><br>
<h2 id="[% course.cat_abbr %]">[% courses.category %]</h2><br><br>

<table> <caption> [% course.caption %] </caption>
<!-- LEVEL 4 -->
<tr style="background-color: Plum">
  <td>L4 (HN)</td>
  <td><input type="checkbox" name="course_file"
             value="[% course.title %]_q1l4v[% courses.version %]"> 1-4</td>
  <td><input type="checkbox"  name="course_file"
             value="[% course.title %]_q2l4v[% course.version %]"> 2-4</td>
  <td><input type="checkbox" name="course_file"
             value="[% course.title %]_q3l4v[% course.version %]"> 3-4</td>
  <td><input type="checkbox" name="course_file"
             value="[% course.title %]_q4l4v[% course.version %]"> 4-4</td>

</table>
[% END %]
<!-- end of Template loop —>


end HTML ————————————— start perl:

my ( $cat_abbr, $category, $title, $caption, $version ); # for Template
my ( @courses, $string ); # for calculating
while ( my $line = <DATA> ) {
    ( $cat_abbr, $category, $title, $version ) = split /\t/, $line;
    chomp $version;
    $caption = $title;
        $caption =~ s{_|\-}{ }xmsg;     # replace _ and - with space
        $caption =~ s{\b(\w)}{\U$1}g;   # impose simple titlecase
        $caption = "$caption" . ' (version ' . "$version" . ')';

    push @courses,
        {
            cat_abbr => $cat_abbr,
            category => $category,
            caption  => $caption,
            title    => $title,
            version  => $version,
        }
}

my %list = (list => \@courses);

# Call Template Toolkit
local $| = 1; # auto flush buffer
my $path = "/big/dom/x$server/www/templates";
my $tt = Template->new(
    {INCLUDE_PATH => "$path"}
);
my $input = 'course_catalog.tt';
my $vars = \%list;
print "Content-Type: text/html\n\n";
$tt->process($input, $vars)
    or die $tt->error();

exit;

# tab separated lines of trial data
__DATA__
engENGLISHenglish2
american-literature1
english-literature1
sciSCIENCEintegrated-science2
biology2
chemistry1
physics1
socSOCIAL STUDIESgeography-and-cultures1
world-history2
american-history1
eleELECTIVEShealth1


Any and all advice will be most welcome!
Rick Triplett

Reply via email to