1 |
package WebPAC::Output::TT; |
2 |
|
3 |
use warnings; |
4 |
use strict; |
5 |
|
6 |
use base qw/WebPAC::Common/; |
7 |
|
8 |
use Template; |
9 |
use List::Util qw/first/; |
10 |
use Data::Dumper; |
11 |
|
12 |
=head1 NAME |
13 |
|
14 |
WebPAC::Output::TT - use Template Toolkit to produce output |
15 |
|
16 |
=head1 VERSION |
17 |
|
18 |
Version 0.01 |
19 |
|
20 |
=cut |
21 |
|
22 |
our $VERSION = '0.01'; |
23 |
|
24 |
=head1 SYNOPSIS |
25 |
|
26 |
Produce output using Template Toolkit. |
27 |
|
28 |
=head1 FUNCTIONS |
29 |
|
30 |
=head2 new |
31 |
|
32 |
Create new instance. |
33 |
|
34 |
my $tt = new WebPAC::Output::TT( |
35 |
include_path => '/path/to/conf/output/tt', |
36 |
filters => { |
37 |
filter_1 => sub { uc(shift) }, |
38 |
}, |
39 |
); |
40 |
|
41 |
By default, Template Toolkit will C<EVAL_PERL> if included in templates. |
42 |
|
43 |
=cut |
44 |
|
45 |
sub new { |
46 |
my $class = shift; |
47 |
my $self = {@_}; |
48 |
bless($self, $class); |
49 |
|
50 |
my $log = $self->_get_logger; |
51 |
|
52 |
# create Template toolkit instance |
53 |
$self->{'tt'} = Template->new( |
54 |
INCLUDE_PATH => $self->{'include_path'}, |
55 |
FILTERS => $self->{'filter'}, |
56 |
EVAL_PERL => 1, |
57 |
); |
58 |
|
59 |
$log->logdie("can't create TT object: $Template::ERROR") unless ($self->{'tt'}); |
60 |
|
61 |
$log->debug("filters defined: ",Dumper($self->{'filter'})); |
62 |
|
63 |
$self ? return $self : return undef; |
64 |
} |
65 |
|
66 |
|
67 |
=head2 apply |
68 |
|
69 |
Create output from in-memory data structure using Template Toolkit template. |
70 |
|
71 |
my $text = $tt->apply( |
72 |
template => 'text.tt', |
73 |
data => $ds |
74 |
); |
75 |
|
76 |
It also has follwing template toolikit filter routies defined: |
77 |
|
78 |
=cut |
79 |
|
80 |
sub apply { |
81 |
my $self = shift; |
82 |
|
83 |
my $args = {@_}; |
84 |
|
85 |
my $log = $self->_get_logger(); |
86 |
|
87 |
foreach my $a (qw/template data/) { |
88 |
$log->logconfess("need $a") unless ($args->{$a}); |
89 |
} |
90 |
|
91 |
=head3 tt_filter_type |
92 |
|
93 |
filter to return values of specified from $ds |
94 |
|
95 |
=cut |
96 |
|
97 |
sub tt_filter_type { |
98 |
my ($data,$type) = @_; |
99 |
|
100 |
die "no data?" unless ($data); |
101 |
$type ||= 'display'; |
102 |
|
103 |
my $default_delimiter = { |
104 |
'display' => '¶<br/>', |
105 |
'index' => '\n', |
106 |
}; |
107 |
|
108 |
return sub { |
109 |
|
110 |
my ($name,$join) = @_; |
111 |
|
112 |
die "no data hash" unless ($data->{'data'} && ref($data->{'data'}) eq 'HASH'); |
113 |
# Hm? Should we die here? |
114 |
return unless ($name); |
115 |
|
116 |
my $item = $data->{'data'}->{$name} || return; |
117 |
|
118 |
my $v = $item->{$type} || return; |
119 |
|
120 |
if (ref($v) eq 'ARRAY') { |
121 |
if ($#{$v} == 0) { |
122 |
$v = $v->[0]; |
123 |
} else { |
124 |
$join = $default_delimiter->{$type} unless defined($join); |
125 |
$v = join($join, @{$v}); |
126 |
} |
127 |
} |
128 |
|
129 |
return $v; |
130 |
} |
131 |
} |
132 |
|
133 |
$args->{'d'} = tt_filter_type($args, 'display'); |
134 |
|
135 |
my $out; |
136 |
|
137 |
$self->{'tt'}->process( |
138 |
$args->{'template'}, |
139 |
$args, |
140 |
\$out |
141 |
) || $log->logconfess( "apply can't process template: ", $self->{'tt'}->error() ); |
142 |
|
143 |
return $out; |
144 |
} |
145 |
|
146 |
=head2 to_file |
147 |
|
148 |
Create output from in-memory data structure using Template Toolkit template |
149 |
to a file. |
150 |
|
151 |
$tt->to_file( |
152 |
file => 'out.txt', |
153 |
template => 'text.tt', |
154 |
data => $ds |
155 |
); |
156 |
|
157 |
=cut |
158 |
|
159 |
sub to_file { |
160 |
my $self = shift; |
161 |
|
162 |
my $args = {@_}; |
163 |
|
164 |
my $log = $self->_get_logger(); |
165 |
|
166 |
my $file = $args->{'file'} || $log->logconfess("need file name"); |
167 |
|
168 |
$log->debug("creating file ",$file); |
169 |
|
170 |
open(my $fh, ">", $file) || $log->logdie("can't open output file '$file': $!"); |
171 |
print $fh $self->output( |
172 |
template => $args->{'template'}, |
173 |
data => $args->{'data'}, |
174 |
) || $log->logdie("print: $!"); |
175 |
close($fh) || $log->logdie("close: $!"); |
176 |
|
177 |
return 1; |
178 |
} |
179 |
|
180 |
|
181 |
=head1 AUTHOR |
182 |
|
183 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
184 |
|
185 |
=head1 COPYRIGHT & LICENSE |
186 |
|
187 |
Copyright 2005 Dobrica Pavlinusic, All Rights Reserved. |
188 |
|
189 |
This program is free software; you can redistribute it and/or modify it |
190 |
under the same terms as Perl itself. |
191 |
|
192 |
=cut |
193 |
|
194 |
1; # End of WebPAC::Output::TT |