#!/usr/bin/perl -w#### HTML template using the TMPL_LOOP tag
##usestrict;
useHTML::Template;
# Send the obligatory Content-Type HTTP header field to the browser.
print"Content-type: text/html\n\n";
my$template = HTML::Template->new(filename => 'loop.tmpl.html');
# a couple of arrays of data to put in a loop:
my@words = qw(I Am Cool);
my@numbers = qw(1 2 3);
my@loop_data = (); # initialize an array to hold your loop
while (@wordsand@numbers) {
my%row_data; # get a fresh hash for the row data
# fill in this row
$row_data{WORD} = shift@words;
$row_data{NUMBER} = shift@numbers;
# the crucial step - push a reference to this row into the loop!
push(@loop_data, \%row_data);
}
# finally, assign the loop data to the loop param, again with a reference:
$template->param(THIS_LOOP => \@loop_data);
# print the template
print $template->output;
The template (loop.tmpl.html):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"lang="en" xml:lang="en"><head><title>TMPL_LOOP example</title></head><body><h1>TMPL_LOOP example</h1><TMPL_LOOP NAME="THIS_LOOP">
Word: <TMPL_VAR NAME="WORD"><br>
Number: <TMPL_VAR NAME="NUMBER"><p></TMPL_LOOP></body></html>
The output:
Word: I
Number: 1
Word: Am
Number: 2
Word: Cool
Number: 3