Debian Codegraph @

examples/objects.html

raw
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__-&gt;follow_best_practice;
43 __PACKAGE__-&gt;mk_accessors(qw(first last age));
44 sub fullname {
45 my $first = $_[0]-&gt;get_first;
46 my $last = $_[0]-&gt;get_last;
47 return &quot;$last, $first&quot;;
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 = &lt;DATA&gt;;
60 seek DATA, 0, SEEK_SET;
61 $perlcode = &lt;DATA&gt;;
62 }
63
64 my $htc = HTML::Template::Compiled-&gt;new(
65 scalarref =&gt; \$template,
66 tagstyle =&gt; [qw(+tt)],
67 use_expressions =&gt; 1,
68 );
69 my $persons = [
70 HTC::Object-&gt;new({first =&gt; &#39;Bart&#39;, last =&gt; &#39;Simpson&#39;, age =&gt; 10, hair =&gt; &#39;yellow&#39;}),
71 HTC::Object-&gt;new({first =&gt; &#39;Maggie&#39;, last =&gt; &#39;Simpson&#39;, age =&gt; 10, hair =&gt; &#39;yellow&#39;}),
72 HTC::Object-&gt;new({first =&gt; &#39;March&#39;, last =&gt; &#39;Simpson&#39;, age =&gt; 42, hair =&gt; &#39;purple&#39;}),
73 HTC::Object-&gt;new({first =&gt; &#39;Homer&#39;, last =&gt; &#39;Simpson&#39;, age =&gt; 42, hair =&gt; &#39;none&#39;}),
74 ];
75 $htc-&gt;param(
76 count =&gt; scalar @$persons,
77 items =&gt; $persons,
78 script =&gt; $0,
79 perlcode =&gt; $perlcode,
80 columns =&gt; [qw/ age hair /],
81 );
82 my $output = $htc-&gt;output;
83 print $output;
84
85 __DATA__
86 &lt;html&gt;&lt;head&gt;&lt;title&gt;HTC example with objects&lt;/title&gt;&lt;/head&gt;
87 &lt;body&gt;
88 &lt;h2&gt;Script: [%= .script %]&lt;/h2&gt;&lt;p&gt;
89 Found [%= .count %] persons:
90 &lt;table&gt;
91 &lt;tr&gt;&lt;th&gt;Name&lt;/th&gt;[%loop .columns %]&lt;th&gt;[%= expr=&quot;ucfirst(_)&quot; %]&lt;/th&gt;&lt;%/loop %&gt;&lt;/tr&gt;
92 [%loop items alias=person %]
93 &lt;tr&gt;
94 &lt;td&gt;[%= fullname %]&lt;/td&gt;
95 [%loop .columns alias=column PRE_CHOMP=3 %]
96 &lt;td&gt;[%= expr=&quot;person{column}&quot; %]&lt;/td&gt;
97 [%/loop PRE_CHOMP=3 %]
98 &lt;/tr&gt;
99 [%/loop items%]
100 &lt;/table&gt;
101 &lt;hr&gt;
102 &lt;h2&gt;The Script:&lt;/h2&gt;
103 &lt;pre&gt;
104 [%= perlcode escape=html %]
105 &lt;/pre&gt;
106 &lt;/body&gt;&lt;/html&gt;
107
108 </pre>
109 </body></html>