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__
eng ENGLISH english 2
american-literature 1
english-literature 1
sci SCIENCE integrated-science 2
biology 2
chemistry 1
physics 1
soc SOCIAL STUDIES geography-and-cultures 1
world-history 2
american-history 1
ele ELECTIVES health 1
Any and all advice will be most welcome!
Rick Triplett