| 1 |
<html><head><title>HTC example with objects</title></head> |
| 2 |
<body> |
| 3 |
<h2>Script: examples/objects.pl</h2><p> |
| 4 |
Found 4 persons: |
| 5 |
<table> |
| 6 |
<tr><th>Name</th><th>Age</th><th>Hair</th></tr> |
| 7 |
|
| 8 |
<tr> |
| 9 |
<td>Simpson, Bart</td> |
| 10 |
<td>10</td> |
| 11 |
<td>yellow</td> |
| 12 |
</tr> |
| 13 |
|
| 14 |
<tr> |
| 15 |
<td>Simpson, Maggie</td> |
| 16 |
<td>10</td> |
| 17 |
<td>yellow</td> |
| 18 |
</tr> |
| 19 |
|
| 20 |
<tr> |
| 21 |
<td>Simpson, March</td> |
| 22 |
<td>42</td> |
| 23 |
<td>purple</td> |
| 24 |
</tr> |
| 25 |
|
| 26 |
<tr> |
| 27 |
<td>Simpson, Homer</td> |
| 28 |
<td>42</td> |
| 29 |
<td>none</td> |
| 30 |
</tr> |
| 31 |
|
| 32 |
</table> |
| 33 |
<hr> |
| 34 |
<h2>The Script:</h2> |
| 35 |
<pre> |
| 36 |
#!/usr/bin/perl |
| 37 |
|
| 38 |
package HTC::Object; |
| 39 |
use strict; |
| 40 |
use warnings; |
| 41 |
use base qw(Class::Accessor); |
| 42 |
__PACKAGE__->follow_best_practice; |
| 43 |
__PACKAGE__->mk_accessors(qw(first last age)); |
| 44 |
sub fullname { |
| 45 |
my $first = $_[0]->get_first; |
| 46 |
my $last = $_[0]->get_last; |
| 47 |
return "$last, $first"; |
| 48 |
} |
| 49 |
|
| 50 |
package main; |
| 51 |
use strict; |
| 52 |
use warnings; |
| 53 |
use HTML::Template::Compiled; |
| 54 |
use Fcntl qw(:seek); |
| 55 |
|
| 56 |
my ($template, $perlcode); |
| 57 |
{ |
| 58 |
local $/; |
| 59 |
$template = <DATA>; |
| 60 |
seek DATA, 0, SEEK_SET; |
| 61 |
$perlcode = <DATA>; |
| 62 |
} |
| 63 |
|
| 64 |
my $htc = HTML::Template::Compiled->new( |
| 65 |
scalarref => \$template, |
| 66 |
tagstyle => [qw(+tt)], |
| 67 |
use_expressions => 1, |
| 68 |
); |
| 69 |
my $persons = [ |
| 70 |
HTC::Object->new({first => 'Bart', last => 'Simpson', age => 10, hair => 'yellow'}), |
| 71 |
HTC::Object->new({first => 'Maggie', last => 'Simpson', age => 10, hair => 'yellow'}), |
| 72 |
HTC::Object->new({first => 'March', last => 'Simpson', age => 42, hair => 'purple'}), |
| 73 |
HTC::Object->new({first => 'Homer', last => 'Simpson', age => 42, hair => 'none'}), |
| 74 |
]; |
| 75 |
$htc->param( |
| 76 |
count => scalar @$persons, |
| 77 |
items => $persons, |
| 78 |
script => $0, |
| 79 |
perlcode => $perlcode, |
| 80 |
columns => [qw/ age hair /], |
| 81 |
); |
| 82 |
my $output = $htc->output; |
| 83 |
print $output; |
| 84 |
|
| 85 |
__DATA__ |
| 86 |
<html><head><title>HTC example with objects</title></head> |
| 87 |
<body> |
| 88 |
<h2>Script: [%= .script %]</h2><p> |
| 89 |
Found [%= .count %] persons: |
| 90 |
<table> |
| 91 |
<tr><th>Name</th>[%loop .columns %]<th>[%= expr="ucfirst(_)" %]</th><%/loop %></tr> |
| 92 |
[%loop items alias=person %] |
| 93 |
<tr> |
| 94 |
<td>[%= fullname %]</td> |
| 95 |
[%loop .columns alias=column PRE_CHOMP=3 %] |
| 96 |
<td>[%= expr="person{column}" %]</td> |
| 97 |
[%/loop PRE_CHOMP=3 %] |
| 98 |
</tr> |
| 99 |
[%/loop items%] |
| 100 |
</table> |
| 101 |
<hr> |
| 102 |
<h2>The Script:</h2> |
| 103 |
<pre> |
| 104 |
[%= perlcode escape=html %] |
| 105 |
</pre> |
| 106 |
</body></html> |
| 107 |
|
| 108 |
</pre> |
| 109 |
</body></html> |