File: objects.pl

package info (click to toggle)
libhtml-template-compiled-perl 1.003-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 772 kB
  • sloc: perl: 4,759; makefile: 5
file content (71 lines) | stat: -rw-r--r-- 1,748 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/perl

package HTC::Object;
use strict;
use warnings;
use base qw(Class::Accessor);
__PACKAGE__->follow_best_practice;
__PACKAGE__->mk_accessors(qw(first last age));
sub fullname {
            my $first = $_[0]->get_first;
            my $last = $_[0]->get_last;
            return "$last, $first";
}

package main;
use strict;
use warnings;
use HTML::Template::Compiled;
use Fcntl qw(:seek);

my ($template, $perlcode);
{
    local $/;
    $template = <DATA>;
    seek DATA, 0, SEEK_SET;
    $perlcode = <DATA>;
}

my $htc = HTML::Template::Compiled->new(
    scalarref => \$template,
    tagstyle => [qw(+tt)],
    use_expressions => 1,
);
my $persons = [
    HTC::Object->new({first => 'Bart',   last => 'Simpson', age => 10, hair => 'yellow'}),
    HTC::Object->new({first => 'Maggie', last => 'Simpson', age => 10, hair => 'yellow'}),
    HTC::Object->new({first => 'March',  last => 'Simpson', age => 42, hair => 'purple'}),
    HTC::Object->new({first => 'Homer',  last => 'Simpson', age => 42, hair => 'none'}),
];
$htc->param(
    count => scalar @$persons,
    items => $persons,
    script => $0,
    perlcode => $perlcode,
    columns => [qw/ age hair /],
);
my $output = $htc->output;
print $output;

__DATA__
<html><head><title>HTC example with objects</title></head>
<body>
<h2>Script: [%= .script %]</h2><p>
Found [%= .count %] persons:
<table>
<tr><th>Name</th>[%loop .columns %]<th>[%= expr="ucfirst(_)" %]</th><%/loop %></tr>
[%loop items alias=person %]
<tr>
    <td>[%= fullname %]</td>
    [%loop .columns alias=column PRE_CHOMP=3 %]
    <td>[%= expr="person{column}" %]</td>
    [%/loop PRE_CHOMP=3 %]
</tr>
[%/loop items%]
</table>
<hr>
<h2>The Script:</h2>
<pre>
[%= perlcode escape=html %]
</pre>
</body></html>