Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Trouble with loops in HTML::Template

by Sherlock (Deacon)
on Jul 10, 2001 at 04:58 UTC ( #95209=perlquestion: print w/ replies, xml ) Need Help??
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.

Comment on Trouble with loops in HTML::Template
Select or Download Code
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://95209]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2014-08-30 19:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls