Use a nested loop in your template file:
<HTML> <HEAD><title>My Billfix Page</title></HEAD> <BODY> <TMPL_LOOP NAME="custname_loop"> <H1>Data Transfer Report for <TMPL_VAR cust_name></H1> <TABLE BORDER="1"> <TR> <TD><B>Period</B></TD> <TD><B>Device Code</B></TD> <TD><B>Port Code</B></TD> <TD><B>Traffic Direction</B></TD> <TD><B>Data Transfer (GB)</B></TD> </TR> <TMPL_LOOP NAME="billfix_loop"> <TR> <TD><TMPL_VAR NAME="lastmonth_period"></TD> <TD><TMPL_VAR NAME="device"></TD> <TD><TMPL_VAR NAME="port"></TD> <TD><TMPL_VAR NAME="direction"></TD> <TD><TMPL_VAR NAME="usage"></TD> </TR> </TMPL_LOOP> </TMPL_LOOP> </TABLE> </BODY> </HTML>
Unfortunately, that means you have to present $template->param() with a very complicated data structure. You could probably hand scribe the data reports before figuring out how to accumulate the data and present it properly to $template->param():
use strict; use warnings; use 5.010; use HTML::Template; use Data::Dumper; my @lines = ( "Bobby June abc 15 north good", "Bobby Sept xyz 20 north fair", "Bobby March xyz 10 north great", "Jenny July def 30 south poor", "Jenny Aug stu 20 south bad", ); my @inner_loop_fields = qw{ lastmonth_period device port direction usage }; my %combined_info_for; #key = customer name #value = array for (@lines) { my($name, @data) = split; my %field_info_for; @field_info_for{@inner_loop_fields} = @data; #assignment to hash slice--allows you to do a mass #assignment to multiple keys at once push @{$combined_info_for{$name}}, \%field_info_for; #add hashes to the customer array--one array for each #customer name, perl magic -> if array doesn't exist, #create an empty one and push the value into the array } #say Dumper(%combined_info_for); #check your work my @outer_loop_fields = qw{ cust_name billfix_loop }; my @outer_loop_info; while ( my($name, $AoH_ref) = each %combined_info_for ) { my %hash_for; @hash_for{@outer_loop_fields} = ($name, $AoH_ref); #assignment to a hash slice again--multiple keys in the hash #are assigned to in one step push @outer_loop_info, \%hash_for; } my $template = HTML::Template->new(filename => "html2.tmpl"); $template->param(custname_loop => \@outer_loop_info); print $template->output();
In reply to Re: Create HTML reports using HTML Template
by 7stud
in thread Create HTML reports using HTML Template
by roborat
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |