LoadHtml
view release on metacpan or search on metacpan
<P>This specifies that the loop should execute once for each element in the array-reference
passed to "index-list". Each value of index-list will become the subscript to use for
argument1 and argument2 in it's respective iteration.</P>
<P>NOTE: If argument1 is a hash-reference instead of an array-reference, then the
keys used for argument1 will be based on the relative position within an imaginary
array built on the fly as "sort(keys(%{$argument1))". For example if the keys for
argument1 (sorted) were "AA", "BB", "CC", and "DD"; and array referenced by index-list contained the values (in this order): (1, 3, 2, 0), then the loop would iterate through the keys in the order of: "BB", then "DD", then "CC", and finally "AA". ...
<P>LoadHTML can also emulate Template::Toolkit's ability to reference subcomponents of
a reference by name. For example:</P>
<BR> my @v ;
<BR> push (@v, {id => 100, name => 'Jack'});
<BR> push (@v, {id => 101, name => 'Jill'});
<BR> push (@v, {id => 102, name => 'Jerry'});
<BR> &loadhtml('template.html', -hashref => \@v);
<BR>
<P>template.html contains:</P>
<br><table>
<BR> <tr><td>ID</td><td>Name</td></tr>
<br> <!LOOP hashref, id, name>
<BR> <tr><td><!:id></td><td><!:name></td></tr>
<br> <!/LOOP>
<br></table>
<br>
<P>This would produce:</P>
<br><table>
<BR> <tr><td>ID</td><td>Name</td></tr>
<BR> <tr><td>100</td><td>Jack</td></tr>
<BR> <tr><td>101</td><td>Jill</td></tr>
<BR> <tr><td>102</td><td>Jerry</td></tr>
<br></table>
<br>
<P>NOTE: "id" and "name" are parameters in the LOOP statement that are NOT DEFINED -
(no argument is passed to them in the call to "loadhtml()"! This results in the
subcomponents of the hashrefs passed to "hashref" (from @v) being used! This is
similar to the way Template::Toolkit works and permits easier conversion of templates
and scripts from that package. Also NOTE: a HASH could have been used in leau of "@v"!</P>
<P>There are four special variables that have meaning within a loop construct:</P>
<UL>
<LI>:# Current increment value. If no increment expression or index list is specified, the loop
is driven by the 1st array or hash argument. In that case, the increment value is the
zero-based iteration of the loop. This value is always numeric and represents the index
subscript of the vectors for the current iteration.
<LI>:* Always the current zero-based iteration of the loop (numeric). Normally, this
is the same as :#, but if an <B>increment expression</B> or <B>index list</B> is specified before the parameters,
then :# is set to each element of the increment expression/index list, whereas :* is ALWAYS 0,1,...
<LI>:% Current key value of the 1st (driving) hash (if the 1st argument is a hash-reference).
Otherwise, this variable is empty (ie. if the loop is driven by an array).
<LI>:^ Always contains the number of iterations (one-based) that the loop will perform.
</UL>
<P>Naming and nesting IF and LOOP constructs.</P>
<P>IF and LOOP constructs can be nested with each other. If nested
within the same construct, however, they must be named (in order for the
parser to match up the proper closing tags). This allows for qualifying the
special variables (:#, :*, etc.) to the desired loop. To name an "IF" or "LOOP"
constuct, simply append an alphanumeric string to the keyword, for example:</P>
<P><!IF2>...<!ELSE2>...<!/IF2></P>
<P> -or-</P>
<P><!LOOP_OUTER>...<!/LOOP_OUTER></P>
<P>The "IF" is named "2", and the "LOOP" is named "_OUTER".</P>
<P>Multi-loop Matrix example:</P>
<P>Consider the following code:</P>
<P>
<BR> my $data = $dbh->selectall_arrayref('select name, address, phone from some_database.table');
<BR> ...
<br> &loadhtml('rate_specials.html',
<br> -colHeaders => [qw(Name Address Phone)],
<br> -matrix => $data,
<br> -names => '$matrix->[*][0]', #THIS IS AN EXAMPLE OF A COLUMN "Slice"!
<br> );
</P>
<P>Now consider the following template code:</P>
<P>
<BR> <table>
<BR> <TR class="heading"><TH>Link</TH><!LOOP_HEADERS colHeaders><TH><!:colHeaders:>Field Header<!:/colHeaders></TH><!/LOOP_HEADERS></TR...
<BR> <!LOOP_ROWS matrix, names>
<BR> <!IF_ODDEVEN1 :#_ROWS % 2><TR class="oddrow"><!ELSE_ODDEVEN1><TR class="evenrow"><!/IF_ODDEVEN1>
<BR> <TD align="center"><A HREF="cgi-bin/someotherpgm.cgi?name=:{names}"><!:names></A></td>
<BR> <!LOOP_COLS matrix><TD><!:matrix:>Field Value<!:/matrix></TD><!/LOOP_COLS>
<BR> </TR>
<BR> <!/LOOP_ROWS>
<BR> </table>
<BR>
</P>
<P>This illustrates how simple it is to combine LoadHTML with DBI (the single call to
DBI::selectall_arrayref fetches all the data from a database query into a two-dimentional
<b>row-major</B> array referenced by $data). This HTML template could handle a variety
of queries, since the number of columns (headers) is also driven by a loop. The "ODDEVEN1"
IF-statement is optional and simply allows the table rows to have alternating colors for readability.
Note the nested loops "_ROWS" (outer) and "_COLS" (inner), both are driven by the two-dimentional array-
referencing parameter "matrix". This will produce a table showing a row for each record
read by the query and each row will contain all three column values.</P>
<P>An extra, but
unnecessary level of complexity was added to this example to illustrate another feature -
the column "slice". Note that the 1st column header is "Link", and the 1st column of
each row is a URL link to "someotherpgm.cgi". The reason for this example is to show access
to the entire column of data represented by the field "names". By specifying an
( run in 0.787 second using v1.01-cache-2.11-cpan-71847e10f99 )