http://www.perlmonks.org?node_id=95209

Sherlock has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: Trouble with loops in HTML::Template
by japhy (Canon) on Jul 10, 2001 at 05:06 UTC
    I worked with Sam Tregar, HTML::Template's author. :)

    Your problem is that
    ArchiveExists => @tipList,
    does not place @tipList in scalar context; you must enforce it:
    ArchiveExists => scalar(@tipList), # or ArchiveExists => 0 + @tipList,


    japhy -- Perl and Regex Hacker