I've been using HTML::Template (using jeffa's HTML::Template Tutorial as a reference) for a while now and I've generally had great success using it. Recently, however, while trying to write what I originally thought would be a very simple script, I ran into a problem that I just can't seem to be able to solve. In certain cases, my template is simply failing to produce any output.


I'm attempting to create a simplistic "Tip of the Week" viewer. That viewer will access a file to get each tip. Every tip is on a separate line. For example, this is what my input file might look like:

Tip 1 Tip 2 Tip 3
The most recent tip is the last one in the file so, in this case, "Tip 3" would be the most recent tip, the "Tip of the Week."

I have a simple .tmpl file which I'll use as my template that looks like this:

<HTML> <HEAD> <TITLE>Tip of the Week</TITLE> </HEAD> <BODY> <TMPL_IF NAME="RecentTip"> <B>Current Tip: </B><TMPL_VAR NAME="RecentTip"><BR><BR> </TMPL_IF> <TMPL_IF NAME="ArchiveExists"> <B>Archived Tips:</B><BR> <TMPL_LOOP NAME="Archive"> <TMPL_VAR NAME="Tip"><BR> </TMPL_LOOP> </TMPL_IF> </BODY> </HTML>
As you can see, there is a section for the most recent tip along with an area where I can list the archived tips. I'll display the code that I'm using and then try to show you the problem I'm having.

#!/usr/bin/perl -w use strict; use HTML::Template; my $template = HTML::Template->new(filename => 'tipDisplay.tmpl'); print "Content-type: text/html\n\n"; my @tipList; open (IFS, "tips.txt"); # Put all of the tips into an array on hash references while (<IFS>) { push @tipList, { 'Tip' => $_ }; } close IFS; # Take the most recent tip out of the array my $tempHashRef = pop @tipList; my %tempHash = %{$tempHashRef}; my $recentTip = $tempHash{'Tip'}; # Supply the template with data $template->param( Archive => \@tipList, ArchiveExists => @tipList, RecentTip => $recentTip, ); print $template->output;
I'm trying to read in all of the tips and put them into an array of hash references, which is what the loop structure in HTML::Template uses. If I have two tips in the file, the script works perfectly. If there is any other number of tips, however, the script fails. Basically, I've narrowed it down to the fact that, if there is only 1 hash reference in @tipList, the script works. If there are more or less, however, it fails.

I've managed to verify, through the following code, that the data is getting into the array @tipList properly:

print "Raw Data<BR><BR>"; foreach (@tipList) { my %hash = %{$_}; print "Array Item: $_ = $hash{'Tip'}<BR>"; } my $numItems = @tipList; print "ArchiveExists: $numItems<BR>"; print "recentTip: $recentTip<BR>";
This code generates something like this for the test file that I gave earlier, which is exactly what I'd expect:
Raw Data Array Item: HASH(0x81a3da8) = Tip 1 Array Item: HASH(0x81a694c) = Tip 2 ArchiveExists: 2 recentTip: Tip 3
However, there is absolutely no output from my template in that case. Can anyone see what I'm doing wrong here?

Thanks,
Sherlock

Skepticism is the source of knowledge as much as knowledge is the source of skepticism.

In reply to Trouble with loops in HTML::Template by Sherlock

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.