Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

comment on

( #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" code into HTML::Template loops by graff
in thread Converting "static" code into HTML::Template loops by hacker

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (6)
    As of 2020-08-14 23:59 GMT
    Find Nodes?
      Voting Booth?
      Which rocket would you take to Mars?

      Results (77 votes). Check out past polls.