LoadHtml

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

<!/LOOP>

This would produce 19 lines of output, the value printed for ":#" would be 10, then 15, 20, ...100.  The tenth, 15th, 20th, 25th, ... and 100th elements of the list passed as argument 2 to LoadHtml() would be displayed.  If that list contained less t...

<!LOOP 20..1|-1>
    ...
<!/LOOP>
 

<!LOOP 1..:argument1 argument2, argument3>

This specifies that the loop should execute argument1 times. Each iteration will correspond to a value of argument2 and argument3 starting with element [1]. argument1 should contain a scaler integer and argument2 and argument3 should be references to...

<!LOOP  5..  argument2, argument3>

This specifies that the loop should execute once for each element of argument2 starting with the 6th one ([5]) and continuing through the last one.

<!LOOP 1,5,2,7 argument1, argument2>

This specifies that the loop should execute 4 times using the 2nd, 6th, 3rd, and 8th values of argument1 and argument2.

<!LOOP index-list argument1, argument2>

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.

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 argume...

LoadHTML can also emulate Template::Toolkit's ability to reference subcomponents of a reference by name. For example:

    my @v ;
    push (@v, {id => 100, name => 'Jack'});
    push (@v, {id => 101, name => 'Jill'});
    push (@v, {id => 102, name => 'Jerry'});
    &loadhtml('template.html', -hashref => \@v);

template.html contains:

<table>
    <tr><td>ID</td><td>Name</td></tr>
    <!LOOP hashref, id, name>
        <tr><td><!:id></td><td><!:name></td></tr>
    <!/LOOP>
</table>

This would produce:

<table>
    <tr><td>ID</td><td>Name</td></tr>
    <tr><td>100</td><td>Jack</td></tr>
    <tr><td>101</td><td>Jill</td></tr>
    <tr><td>102</td><td>Jerry</td></tr>
</table>

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 ...

There are four special variables that have meaning within a loop construct:

    * :# 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...
    * :* Always the current zero-based iteration of the loop (numeric). Normally, this is the same as :#, but if an increment expression or index list is specified before the parameters, then :# is set to each element of the increment expression/inde...
    * :% 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).
    * :^ Always contains the number of iterations (one-based) that the loop will perform. 

Naming and nesting IF and LOOP constructs.

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...

<!IF2>...<!ELSE2>...<!/IF2>

    -or-

<!LOOP_OUTER>...<!/LOOP_OUTER>

The "IF" is named "2", and the "LOOP" is named "_OUTER".

Multi-loop Matrix example:

Consider the following code:


    my $data = $dbh->selectall_arrayref('select name, address, phone from some_database.table');
    ...
    &loadhtml('rate_specials.html',
        -colHeaders => [qw(Name Address Phone)],
        -matrix => $data,
        -names => '$matrix->[*][0]', #THIS IS AN EXAMPLE OF A COLUMN "Slice"!
    );

Now consider the following template code:


    <table>
         <TR class="heading"><TH>Link</TH><!LOOP_HEADERS colHeaders><TH><!:colHeaders:>Field Header<!:/colHeaders></TH><!/LOOP_HEADERS></TR>
         <!LOOP_ROWS matrix, names>
             <!IF_ODDEVEN1 :#_ROWS % 2><TR class="oddrow"><!ELSE_ODDEVEN1><TR class="evenrow"><!/IF_ODDEVEN1>
                 <TD align="center"><A HREF="cgi-bin/someotherpgm.cgi?name=:{names}"><!:names></A></td>
                 <!LOOP_COLS matrix><TD><!:matrix:>Field Value<!:/matrix></TD><!/LOOP_COLS>
             </TR>
         <!/LOOP_ROWS>
    </table>

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 row-major array referenced by $data). This HTML template could handle a varie...

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 ...

Note also, that is is not limited to 2 dimensions or to array-references. The number of dimensions is not physically limited, but can be any number and combination of array and or hash-references. The trick is that there normally must be a nested loo...

The above example could have also been acomplished without the slice by using an inner loop (called "NAMES" below) that only referenced the desired (zero-th) element (only iterates once (0..0) unrolling the zeroth column (inner dimension) element of ...


    <table>
         <TR class="heading"><TH>Link</TH><!LOOP_HEADERS colHeaders><TH><!:colHeaders:>Field Header<!:/colHeaders></TH><!/LOOP_HEADERS></TR>
         <!LOOP_ROWS matrix>
             <!IF_ODDEVEN1 :#_ROWS % 2><TR class="oddrow"><!ELSE_ODDEVEN1><TR class="evenrow"><!/IF_ODDEVEN1>
                 <!LOOP_NAMES 0..0 matrix><TD align="center"><A HREF="cgi-bin/someotherpgm.cgi?name=:{matrix}"><!:matrix></A></td><!/LOOP_NAMES>/
                 <!LOOP_COLS matrix><TD><!:matrix:>Field Value<!:/matrix></TD><!/LOOP_COLS>
             </TR>
         <!/LOOP_ROWS>
    </table>

"SELECTLIST" Statement:



( run in 0.559 second using v1.01-cache-2.11-cpan-71847e10f99 )