1 |
dpavlin |
16 |
package WebPAC::Output::TT; |
2 |
dpavlin |
1 |
|
3 |
|
|
use warnings; |
4 |
|
|
use strict; |
5 |
|
|
|
6 |
dpavlin |
16 |
use base qw/WebPAC::Common/; |
7 |
|
|
|
8 |
|
|
use Template; |
9 |
|
|
use Data::Dumper; |
10 |
|
|
|
11 |
dpavlin |
1 |
=head1 NAME |
12 |
|
|
|
13 |
dpavlin |
16 |
WebPAC::Output::TT - use Template Toolkit to produce output |
14 |
dpavlin |
1 |
|
15 |
|
|
=head1 VERSION |
16 |
|
|
|
17 |
|
|
Version 0.01 |
18 |
|
|
|
19 |
|
|
=cut |
20 |
|
|
|
21 |
|
|
our $VERSION = '0.01'; |
22 |
|
|
|
23 |
|
|
=head1 SYNOPSIS |
24 |
|
|
|
25 |
dpavlin |
16 |
Produce output using Template Toolkit. |
26 |
dpavlin |
1 |
|
27 |
dpavlin |
16 |
=head1 FUNCTIONS |
28 |
dpavlin |
1 |
|
29 |
dpavlin |
16 |
=head2 new |
30 |
dpavlin |
1 |
|
31 |
dpavlin |
16 |
Create new instance. |
32 |
dpavlin |
1 |
|
33 |
dpavlin |
16 |
my $tt = new WebPAC::Output::TT( |
34 |
|
|
include_path => '/path/to/conf/output/tt', |
35 |
|
|
filters => { |
36 |
|
|
filter_1 => sub { uc(shift) }, |
37 |
|
|
}, |
38 |
|
|
); |
39 |
dpavlin |
1 |
|
40 |
dpavlin |
16 |
By default, Template Toolkit will C<EVAL_PERL> if included in templates. |
41 |
dpavlin |
1 |
|
42 |
dpavlin |
16 |
=cut |
43 |
dpavlin |
1 |
|
44 |
dpavlin |
16 |
sub new { |
45 |
|
|
my $class = shift; |
46 |
|
|
my $self = {@_}; |
47 |
|
|
bless($self, $class); |
48 |
dpavlin |
1 |
|
49 |
dpavlin |
16 |
my $log = $self->_get_logger; |
50 |
|
|
|
51 |
|
|
# create Template toolkit instance |
52 |
|
|
$self->{'tt'} = Template->new( |
53 |
|
|
INCLUDE_PATH => $self->{'include_path'}, |
54 |
|
|
FILTERS => $self->{'filter'}, |
55 |
|
|
EVAL_PERL => 1, |
56 |
|
|
); |
57 |
|
|
|
58 |
|
|
$log->logdie("can't create TT object: $Template::ERROR") unless ($self->{'tt'}); |
59 |
|
|
|
60 |
|
|
$log->debug("filters defined: ",Dumper($self->{'filter'})); |
61 |
|
|
|
62 |
|
|
$self ? return $self : return undef; |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
|
66 |
|
|
=head2 apply |
67 |
|
|
|
68 |
|
|
Create output from in-memory data structure using Template Toolkit template. |
69 |
|
|
|
70 |
|
|
my $text = $tt->apply( template => 'text.tt', data => @ds ); |
71 |
|
|
|
72 |
dpavlin |
1 |
=cut |
73 |
|
|
|
74 |
dpavlin |
16 |
sub apply { |
75 |
|
|
my $self = shift; |
76 |
|
|
|
77 |
|
|
my $args = {@_}; |
78 |
|
|
|
79 |
|
|
my $log = $self->_get_logger(); |
80 |
|
|
|
81 |
|
|
foreach my $a (qw/template data/) { |
82 |
|
|
$log->logconfess("need $a") unless ($args->{$a}); |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
my $out; |
86 |
|
|
|
87 |
|
|
$self->{'tt'}->process( |
88 |
|
|
$args->{'template'}, |
89 |
|
|
$args, |
90 |
|
|
\$out |
91 |
|
|
) || $log->logconfess( "apply can't process template: ", $self->{'tt'}->error() ); |
92 |
|
|
|
93 |
|
|
return $out; |
94 |
dpavlin |
1 |
} |
95 |
|
|
|
96 |
dpavlin |
16 |
=head2 to_file |
97 |
dpavlin |
1 |
|
98 |
dpavlin |
16 |
Create output from in-memory data structure using Template Toolkit template |
99 |
|
|
to a file. |
100 |
|
|
|
101 |
|
|
$tt->to_file( |
102 |
|
|
file => 'out.txt', |
103 |
|
|
template => 'text.tt', |
104 |
|
|
data => @ds |
105 |
|
|
); |
106 |
|
|
|
107 |
dpavlin |
1 |
=cut |
108 |
|
|
|
109 |
dpavlin |
16 |
sub to_file { |
110 |
|
|
my $self = shift; |
111 |
|
|
|
112 |
|
|
my $args = {@_}; |
113 |
|
|
|
114 |
|
|
my $log = $self->_get_logger(); |
115 |
|
|
|
116 |
|
|
my $file = $args->{'file'} || $log->logconfess("need file name"); |
117 |
|
|
|
118 |
|
|
$log->debug("creating file ",$file); |
119 |
|
|
|
120 |
|
|
open(my $fh, ">", $file) || $log->logdie("can't open output file '$file': $!"); |
121 |
|
|
print $fh $self->output( |
122 |
|
|
template => $args->{'template'}, |
123 |
|
|
data => $args->{'data'}, |
124 |
|
|
) || $log->logdie("print: $!"); |
125 |
|
|
close($fh) || $log->logdie("close: $!"); |
126 |
|
|
|
127 |
|
|
return 1; |
128 |
dpavlin |
1 |
} |
129 |
|
|
|
130 |
dpavlin |
16 |
|
131 |
dpavlin |
1 |
=head1 AUTHOR |
132 |
|
|
|
133 |
|
|
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
134 |
|
|
|
135 |
|
|
=head1 COPYRIGHT & LICENSE |
136 |
|
|
|
137 |
|
|
Copyright 2005 Dobrica Pavlinusic, All Rights Reserved. |
138 |
|
|
|
139 |
|
|
This program is free software; you can redistribute it and/or modify it |
140 |
|
|
under the same terms as Perl itself. |
141 |
|
|
|
142 |
|
|
=cut |
143 |
|
|
|
144 |
dpavlin |
16 |
1; # End of WebPAC::Output::TT |