Red Antigua Logo
Yet another piece of web.
Ads by Goooooogle
Search this site (by Google)
Tools    (top)
Your IP
Check a site for broken links
(W3C)

Perl modules    (top)
Tree::Numbered::Tools
(CPAN)
Perl tutorials    (top)
Perl modules
HTML::Template
CGI::Application
Mail::POP3Client
Mail::Send
MIME::Tools
Cookies with CGI::Application
Upload files with CGI::Application
Download files with CGI::Application
Redirect with CGI::Application
CPAN shell
Install DBD::mysql from the CPAN shell
Perl trim function
Validate an IP with Perl
Run suid Perl scripts under Apache
Perl taint mode
Perl date functions with Date::Calc

In Spanish
Curso de Perl

C tutorials    (top)
C - Introduction
C - Absolute beginner's Emacs
C - Examples for beginners
C - Makefile examples
C - Autotools examples
Server configurations    (top)
DNS
Apache
Apache Authentication and Access Control
mod_perl on FreeBSD
MySQL
MySQL add account
phpMyAdmin
Squid
DHCP

UNIX on Windows    (top)
Apache setup on Windows
MySQL setup on Windows
PHP setup on Windows
Perl setup on Windows
Emacs setup on Windows
UnxUtils
PuTTY
WinSCP
GIMP on Windows
MinGW - gcc on Windows
MSYS - UNIX-styled shell on Windows
msysDTK - autotools on Windows
GDB for MinGW on Windows

Misc. FreeBSD/UNIX    (top)
CD and DVD creation on FreeBSD using 'k3b' on FreeBSD
'portupgrade' on FreeBSD
'ipf' on FreeBSD
'pf' on FreeBSD
'su' on FreeBSD
Mount an ISO image under FreeBSD
Load the correct sound driver under FreeBSD without knowing what sound card you are using
Simultaneous sound channels on FreeBSD
FreeBSD network stuff
DOS-to-UNIX file conversion
favicon.ico on UNIX
Emacs tips
Sendmail tips
GKrellm
Command Line Calculator
Save multimedia streams with 'mplayer'
xargs - solution to 'Argument list too long'
Process multiple images from the command line using 'ImageMagick'
Turn the system bell off under X Windows
Process each line in an input file from the command line (or in a shell script)
How to keep a program running in the background using 'nohup'
How to remove symbolic links in the current directory using 'find' and 'rm'
How to remove Emacs backup files in the current directory and all subdirectories using 'find' and 'rm'
How to execute .profile without logging in
Configure X to handle non-English characters
How to move /var to /usr/var

Redirect a web page    (top)
Redirect to another web page
Apache redirect
C redirect
Perl redirect
PHP redirect
HTML redirect
JavaScript redirect

Javascript    (top)
Trim function
Login form
Register form
Popup window

Virus list    (top)
Latest 10 viruses - Sophos

Off topic    (top)
Personal home links

HTML::Template Tutorial - Nested <TMPL_LOOP>s
HTML::Template - Nested <TMPL_LOOP>s
IndexPreviousNext
5. Nested <TMPL_LOOP>s

The code (loop-nested.pl):
#!/usr/bin/perl -wT
##
##  HTML template using nested TMPL_LOOPs
##
use strict;
use HTML::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-nested.tmpl.html');

# A couple of arrays of data to put in a loop:
my @husbands = qw(Abraham Apollo Olof Achilles José&nbsp;Luis);
my @wives = qw(Mary Hyacinth Lisbet Patroclus Sonsoles);
my @no_of_children = qw(4 1 3 0 2);
my @children = qw(Robert Edward William Thomas Discus Joakim Mårten Mattias Laura Alba);

my @loop_data = ();            # initialize an array to hold your loop

while (@husbands and @wives)
{
    my %row_data;               # get a fresh hash for the row data
    # Fill in this row
    $row_data{HUSBAND} = shift @husbands;
    $row_data{WIFE} = shift @wives;
    $row_data{NO_OF_CHILDREN} = shift @no_of_children;
    # Here goes the inner loop data
    my @inner_loop_data = ();            # initialize an array to hold your loop
    for (my $i = 0; $i < $row_data{NO_OF_CHILDREN}; $i++)
    {
        my %nested_row_data;               # get a fresh hash for the nested row data
        $nested_row_data{CHILD} = shift @children;
        # The crucial step - push a reference to this row into the inner loop!
        push(@inner_loop_data, \%nested_row_data);
    }
    $row_data{INNER_LOOP} = \@inner_loop_data;
    # 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(NESTED_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">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
  <title>Example with nested TMPL_LOOPs</title>
</head>

<body>
<h1>Example with nested TMPL_LOOPs</h1>

<TMPL_LOOP NAME="NESTED_LOOP">
<div style="padding-left: 20px">
Husband: <TMPL_VAR NAME="HUSBAND"><br>
<TMPL_IF "NO_OF_CHILDREN">
Wife: <TMPL_VAR NAME="WIFE"><br>
<TMPL_ELSE>
Husband: <TMPL_VAR NAME="WIFE"><br>  
</TMPL_IF>
Children: <TMPL_VAR NAME="NO_OF_CHILDREN"><br>
</div>

<TMPL_IF "NO_OF_CHILDREN">
<div style="padding-left: 50px">
Name:<br>
<TMPL_LOOP NAME="INNER_LOOP">
<div style="padding-left: 30px">
  <TMPL_VAR NAME="CHILD"><br>
</div>
</TMPL_LOOP>
</div>
</TMPL_IF>

<br>
</TMPL_LOOP>

</body>
</html>

The output:
Husband: Abraham
Wife: Mary
Children: 4
Name:
  Robert
  Edward
  William
  Thomas

Husband: Apollo
Wife: Hyacinth
Children: 1
Name:
  Discus

Husband: Olof
Wife: Lisbet
Children: 3
Name:
  Joakim
  Mårten
  Mattias

Husband: Achilles
Husband: Patroclus
Children: 0

Husband: José Luis
Wife: Sonsoles
Children: 2
Name:
  Laura
  Alba

Read more:
The <TMPL_LOOP> section in the HTML::Template documentation:
http://search.cpan.org/dist/HTML-Template/Template.pm#TMPL_LOOP
The <TMPL_LOOP> examples in the Perlmonks tutorial:
http://www.perlmonks.org/?node=65642
IndexPreviousNext
Last modified: Thu Aug 23 12:24:59 Romance Daylight Time 2007