1 |
package WebPAC::Output::html; |
package WebPAC::Output::TT; |
2 |
|
|
3 |
use warnings; |
use warnings; |
4 |
use strict; |
use strict; |
5 |
|
|
6 |
|
use base qw/WebPAC::Common/; |
7 |
|
|
8 |
|
use Template; |
9 |
|
use Data::Dumper; |
10 |
|
|
11 |
=head1 NAME |
=head1 NAME |
12 |
|
|
13 |
WebPAC::Output::html - The great new WebPAC::Output::html! |
WebPAC::Output::TT - use Template Toolkit to produce output |
14 |
|
|
15 |
=head1 VERSION |
=head1 VERSION |
16 |
|
|
22 |
|
|
23 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
24 |
|
|
25 |
Quick summary of what the module does. |
Produce output using Template Toolkit. |
26 |
|
|
27 |
Perhaps a little code snippet. |
=head1 FUNCTIONS |
28 |
|
|
29 |
use WebPAC::Output::html; |
=head2 new |
30 |
|
|
31 |
my $foo = WebPAC::Output::html->new(); |
Create new instance. |
|
... |
|
32 |
|
|
33 |
=head1 EXPORT |
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 |
|
|
40 |
A list of functions that can be exported. You can delete this section |
By default, Template Toolkit will C<EVAL_PERL> if included in templates. |
|
if you don't export anything, such as for a purely object-oriented module. |
|
41 |
|
|
42 |
=head1 FUNCTIONS |
=cut |
43 |
|
|
44 |
=head2 function1 |
sub new { |
45 |
|
my $class = shift; |
46 |
|
my $self = {@_}; |
47 |
|
bless($self, $class); |
48 |
|
|
49 |
|
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 |
=cut |
$log->debug("filters defined: ",Dumper($self->{'filter'})); |
61 |
|
|
62 |
sub function1 { |
$self ? return $self : return undef; |
63 |
} |
} |
64 |
|
|
65 |
=head2 function2 |
|
66 |
|
=head2 apply |
67 |
|
|
68 |
|
Create output from in-memory data structure using Template Toolkit template. |
69 |
|
|
70 |
|
my $text = $tt->apply( |
71 |
|
template => 'text.tt', |
72 |
|
data => @ds |
73 |
|
); |
74 |
|
|
75 |
=cut |
=cut |
76 |
|
|
77 |
sub function2 { |
sub apply { |
78 |
|
my $self = shift; |
79 |
|
|
80 |
|
my $args = {@_}; |
81 |
|
|
82 |
|
my $log = $self->_get_logger(); |
83 |
|
|
84 |
|
foreach my $a (qw/template data/) { |
85 |
|
$log->logconfess("need $a") unless ($args->{$a}); |
86 |
|
} |
87 |
|
|
88 |
|
my $out; |
89 |
|
|
90 |
|
$self->{'tt'}->process( |
91 |
|
$args->{'template'}, |
92 |
|
$args, |
93 |
|
\$out |
94 |
|
) || $log->logconfess( "apply can't process template: ", $self->{'tt'}->error() ); |
95 |
|
|
96 |
|
return $out; |
97 |
} |
} |
98 |
|
|
99 |
=head1 AUTHOR |
=head2 to_file |
100 |
|
|
101 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
Create output from in-memory data structure using Template Toolkit template |
102 |
|
to a file. |
103 |
|
|
104 |
|
$tt->to_file( |
105 |
|
file => 'out.txt', |
106 |
|
template => 'text.tt', |
107 |
|
data => @ds |
108 |
|
); |
109 |
|
|
110 |
|
=cut |
111 |
|
|
112 |
|
sub to_file { |
113 |
|
my $self = shift; |
114 |
|
|
115 |
|
my $args = {@_}; |
116 |
|
|
117 |
|
my $log = $self->_get_logger(); |
118 |
|
|
119 |
=head1 BUGS |
my $file = $args->{'file'} || $log->logconfess("need file name"); |
120 |
|
|
121 |
Please report any bugs or feature requests to |
$log->debug("creating file ",$file); |
|
C<bug-webpac-output-html@rt.cpan.org>, or through the web interface at |
|
|
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WebPAC>. |
|
|
I will be notified, and then you'll automatically be notified of progress on |
|
|
your bug as I make changes. |
|
122 |
|
|
123 |
=head1 ACKNOWLEDGEMENTS |
open(my $fh, ">", $file) || $log->logdie("can't open output file '$file': $!"); |
124 |
|
print $fh $self->output( |
125 |
|
template => $args->{'template'}, |
126 |
|
data => $args->{'data'}, |
127 |
|
) || $log->logdie("print: $!"); |
128 |
|
close($fh) || $log->logdie("close: $!"); |
129 |
|
|
130 |
|
return 1; |
131 |
|
} |
132 |
|
|
133 |
|
|
134 |
|
=head1 AUTHOR |
135 |
|
|
136 |
|
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
137 |
|
|
138 |
=head1 COPYRIGHT & LICENSE |
=head1 COPYRIGHT & LICENSE |
139 |
|
|
144 |
|
|
145 |
=cut |
=cut |
146 |
|
|
147 |
1; # End of WebPAC::Output::html |
1; # End of WebPAC::Output::TT |