Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Converting "static" CGI.pm code into HTML::Template loops

by graff (Chancellor)
on Sep 17, 2007 at 00:55 UTC ( [id://639309]=note: print w/replies, xml ) Need Help??


in reply to Converting "static" CGI.pm code into HTML::Template loops

Just think of each row in your output table as one interation of a single <TMPL_LOOP NAME="..."> ... </TMPL_LOOP>.

Your foreach loop, instead of printing things, will be pushing elements onto an array of hashes, one hash for each iteration / table row, where the hash keys will be the names assigned to the specific TMPL_VAR's that you put inside the TMPL_LOOP portion of the template.

I think the relevant part of your template would probably look something like this:

<table border="1"> <tr> <th>Language</th> <th>Last updated</th> </tr> <TMPL_LOOP NAME="LANG_ROWS"> <tr> <td align="right"> <a href=<TMPL_VAR NAME="LINK_viewer"> title=<TMPL_VAR NAME="SIZE_view +er">> <TMPL_VAR NAME="TEXT_viewer"></a> (<a href=<TMPL_VAR NAME="LINK_ppi"> title=<TMPL_VAR NAME="SIZE_ppi">>p +pi</a>) </td> <td align="right"><TMPL_VAR NAME="FILEDATE"></td> </tr> </TMPL_LOOP> </table>
Then, your foreach loop just needs to make sure that a hash is filled with values for the six TMPL_VAR names (FILEDATE, TEXT_viewer, SIZE_viewer, LINK_viewer, SIZE_ppi, LINK_ppi), and the hash is pushed onto an array for use with the template -- something like this might suffice:
my @table_data; for ( 0 .. $#iso ) { my %row_data = (); for my $file ( qw/viewer ppi/ ) { my $file_path = join( "_", "$root/$file", "$iso[$_].prc" ); last unless stat( $file_path ); $row_data{"LINK_$file"} = "\"$snapdir/$file_path\""; $row_data{"SIZE_$file"} = sprintf( "\"%s bytes\"", insert_commas( -s _ )); if ( $file eq 'viewer' ) { $row_data{"FILEDATE"} = strftime( "%D %r", localtime( $^T - ( -M _ * 3600 * 24 + ))); $row_data{"TEXT_$file"} = sprintf( "%-12s", $lang[$_] ); } } push @table_data, { %row_data } if ( scalar( keys %row_data ) == 6 + ); }
(none of the above has been tested -- updated to fix indenting, and to avoid pushing empty/incomplete hashes onto the array by checking the number of hash keys)

As mentioned in an earlier reply, you should be able to work out the rest by reading the fine manual for HTML::Template.

I saw a lot of unnecessary work going on in the original foreach loop, and tried to eliminate it in the suggested code. I think my version does basically the same thing...

... Except I was baffled by (and did not try to repeat) the OP's use of substr() to create the link targets for the href attributes. As a rule, I'd say "just don't do that." It's really a bad idea, because it creates a sort of brittleness, obfuscation and dependency on external details (like changes to path names) that make the script harder to maintain. (Why use the values 34 or 35? How would they change if the file path changes?)

There is a better way to get the intended result, which does not involve using substr() with "magic numbers".

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://639309]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-19 22:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found