[Melbourne-pm] Looping with HTML::Template

Andrew Dent sgc294 at internode.on.net
Mon Oct 27 03:41:37 PDT 2008


Further to the thread about the sub called 'Strange things about '0'.
I have the section of Perl below to build up an array of hashes that 
will get used by HTML::Template after sql_engine finishes.
At the moment it is hardcoded to only produce 6 fields in the row.

How do I make the .html file flexible so that in the Perl code I can 
replace the 6 with a variable like $Num_of_fields allowing me to choose 
at run time how wide the HTML table is?
Or is there a different module/technique I should use to get this type 
of functionality?

BTW: That 'FooCode' field below is where one of values of foocode from 
the other thread will appear and may get filtered on.

sub sql_engine {
    elsif ($ret_type eq 'array_hash') {
        my $cnt = 0;
        my @loop_data;
        while (my @data = $report->fetchrow_array) {
            my $cell = 0; # This number is used as a temp variable name 
in the .html file
            my %row_data; # use a hash to hold the data for each row 
that HTML::Template will display.
            # populate the hash.
            while ($cell < 6) {
                $row_data{$cell} = shift @data;
            push (@loop_data, \%row_data);
        return \@loop_data;       

Contents of the .html file

    <table border =1 cellpadding = 4 cellspacing =0>
            <th bgcolor =#fffbc6>Model</th>
            <th bgcolor =#fffbc6>Serial Number</th>
            <th bgcolor =#fffbc6>Customer Number</th>
            <th bgcolor =#fffbc6>Asset ID</th>
            <th bgcolor =#fffbc6>FooCode</th>
            <th bgcolor =#fffbc6>Standard Code</th>
        <TMPL_LOOP NAME='blankassets'>
                <td><TMPL_VAR name="0"></td>
                <td><TMPL_VAR name="1"></td>
                <td><TMPL_VAR name="2"></td>
                <td><TMPL_VAR name="3"></td>
                <td><TMPL_VAR name="4"></td>
                <td><TMPL_VAR name="5"></td>


Andrew Dent

More information about the Melbourne-pm mailing list