Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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".


In reply to Re: Converting "static" CGI.pm code into HTML::Template loops by graff
in thread Converting "static" CGI.pm code into HTML::Template loops by hacker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-03-28 08:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found