Re: [htmltmpl] HTML::Template Loop Issue
Brought to you by:
samtregar
|
From: Alex T. <al...@ac...> - 2010-04-08 22:44:50
|
On Thu, 8 Apr 2010 17:20:36 -0400, Shawn Scott wrote
> Good Afternoon,
>
> I have been using HTML::Template for some time and have recently run
> into an starnge issue. I have the following code
>
> my @error = @_;
> my @error_loop = ();
> my %error_data;
> print header;
> foreach (@error) {
> $error_data{ERROR_DATA} = $_;
> push(@error_loop, \%error_data);
> }
> my $template = HTML::Template->new(filename =>
> "$html_root/signup_error.html");
> $template->param(ERROR_LOOP => \@error_loop);
> print $template->output;
Each entry in your @error_loop is looking by reference at the %error_data hash
key ERROR_DATA. So you are not storing the actual data in @error_loop, just a
reference to the ERROR_DATA hash key in %error_data. That hash key takes the
last value of the @error_loop during the foreach - and then all the references
pointing to that key look the same.
You are using a lot of variables, sigils, metacharacters, and capitalization
that you don't need. It makes the code hard to read. It could be cleaner:
my @errors = @_;
my $template = HTML::Template->new(filename => "errors.html");
$template->param (
errorloop => [
map { { error => $_ } } @errors
],
);
print header();
print $template->output;
untested.
HTH,
Alex
|