I considered the loop but the output inside the while is not making sense to me. Here is a sample template if I would use the loop tag.
<table>
<tr>
<td><TMPL_VAR NAME="NAME"></td>
</tr>
<TMPL_LOOP NAME=DATA>
<tr>
<td><TMPL_VAR NAME="NUMBER"></td>
<td><TMPL_VAR NAME="DATE"></td>
<td><TMPL_VAR NAME="COL"></td>
<td><TMPL_VAR NAME="FIRST"></td>
<td><TMPL_VAR NAME="LAST"></td>
</tr>
</TMPL_LOOP>
</table>
I am using without the loop tag:
<table>
<tr>
<td><TMPL_VAR NAME="NAME"></td>
</tr>
<tr>
<td><TMPL_VAR NAME="NUMBER"></td>
<td><TMPL_VAR NAME="DATE"></td>
<td><TMPL_VAR NAME="COL"></td>
<td><TMPL_VAR NAME="FIRST"></td>
<td><TMPL_VAR NAME="LAST"></td>
</tr>
</table>
One output example:
<table>
<tr>
<td>John Doe</td>
</tr>
<tr>
<td>1111</td>
<td>04/28/2014</td>
<td>Code 4</td>
<td>A</td>
<td>B</td>
</tr>
<tr>
<td>234</td>
<td>04/28/2014</td>
<td>Code 5</td>
<td>A</td>
<td>C</td>
</tr>
<tr>
<td>6678</td>
<td>05/28/2014</td>
<td>Code 9</td>
<td>B</td>
<td>K</td>
</tr>
</table>
Another:
<table>
<tr>
<td>Mary ANn</td>
</tr>
<tr>
<td>88766</td>
<td>04/28/2014</td>
<td>Code AX</td>
<td>Q</td>
<td>M</td>
</tr>
</table>
I hope it's a little clear, thanks! | [reply] [d/l] [select] |
That helps, thank you. Although it's possible to avoid the TMPL_LOOP tag by generating your output manually, that doesn't make sense because you're already using HTML::Template the TMPL_LOOP tag seems to be exactly what you need to get the output you want. Here's a an approach that continues to use the module.
Take a look at the TMPL_LOOP documentation, it includes examples. Inside the while loop you'll need to build a data structure, an "array of hashes", see for example here: Generation of an ARRAY OF HASHES. Here's one way you might do it, assuming the NAME stays the same:
$data_tmpl->param(NAME => $name);
my @data;
while ( my ( $key, $entry ) = each %{ $more_data } ) {
push @data, {
NUMBER => $entry->{NUMBER},
DATE => $entry->{DATE},
COL => $entry->{COL},
FIRST => $entry->{FIRST},
LAST => $entry->{NAME},
};
}
# the following line is JUST FOR DEBUGGING, remove for production code
use Data::Dumper; print STDERR Dumper(\@data); # show @data on STDERR
$template->param(DATA => \@data);
# now you can write your file
(note: untested except for syntax check)
A few things still aren't quite clear: Is the "NAME" the same for every entry? If not, you may need to wrap your TMPL_LOOP inside another TMPL_LOOP, the outer one looping over names with the inner one looping over the data for that name (i.e. nested loops). In the Perl code you would also need to put your while loop inside another while loop. From your sample code it's not quite clear if there are multiple names per file and how the data for different names would be fetched, it would also help to see some of your input data. You can pretty-print it like this: use Data::Dumper; print Dumper($more_data); Also, what do you want the filename(s) to be? In your sample code you're basing them on $key, implying that you want one file per data record?
| [reply] [d/l] [select] |
I see, but I cant get the value for $entry->{'NAME'} unless I am inside of the WHILE and since there will be lots of files been created I need to use the value of $key to name these files. The value of $entry->{'NAME'} is one per file, it does not need to be in a loop.
Once I have all the data pushed into the @data array how it would be possible to create the files for each $entry->{'NAME'} ?
| [reply] [d/l] [select] |
while ( my ($key,$name) = get_key_and_name() ) {
my @data;
while ( my ($entry) = get_data_for_key($key) ) {
push @data, { NUMBER => $entry->{NUMBER}, etc=>"etc" };
}
my $data_tmpl = HTML::Template->new(filename => 'data.tmpl');
$data_tmpl->param(NAME => $name, DATA => \@data);
write_file("/location/$key.html", $data_tmpl->output);
}
(once again untested and based only on the docs - just trying to show the concept of nested while loops here)
Without knowing what $more_data looks like, I can't say what the placeholders get_key_and_name() and get_data_for_key() should look like.
| [reply] [d/l] [select] |