Sherlock has asked for the wisdom of the Perl Monks concerning the following question:
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."Tip 1 Tip 2 Tip 3
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.<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>
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.#!/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;
This code generates something like this for the test file that I gave earlier, which is exactly what I'd expect: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>";
However, there is absolutely no output from my template in that case. Can anyone see what I'm doing wrong here?Raw Data Array Item: HASH(0x81a3da8) = Tip 1 Array Item: HASH(0x81a694c) = Tip 2 ArchiveExists: 2 recentTip: Tip 3
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Trouble with loops in HTML::Template
by japhy (Canon) on Jul 10, 2001 at 05:06 UTC |