1 package WebPAC::Output::TT;
6 use base qw/WebPAC::Common/;
9 use List::Util qw/first/;
14 WebPAC::Output::TT - use Template Toolkit to produce output
22 our $VERSION = '0.01';
26 Produce output using Template Toolkit.
34 my $tt = new WebPAC::Output::TT(
35 include_path => '/path/to/conf/output/tt',
37 filter_1 => sub { uc(shift) },
41 By default, Template Toolkit will C<EVAL_PERL> if included in templates.
50 my $log = $self->_get_logger;
52 # create Template toolkit instance
53 $self->{'tt'} = Template->new(
54 INCLUDE_PATH => $self->{'include_path'},
55 FILTERS => $self->{'filter'},
59 $log->logdie("can't create TT object: $Template::ERROR") unless ($self->{'tt'});
61 $log->debug("filters defined: ",Dumper($self->{'filter'}));
63 $self ? return $self : return undef;
69 Create output from in-memory data structure using Template Toolkit template.
71 my $text = $tt->apply(
72 template => 'text.tt',
76 It also has follwing template toolikit filter routies defined:
85 my $log = $self->_get_logger();
87 foreach my $a (qw/template data/) {
88 $log->logconfess("need $a") unless ($args->{$a});
93 filter to return values of specified from @ds
98 my ($data,$type) = @_;
100 die "no data?" unless ($data);
103 my $default_delimiter = {
104 'display' => '¶<br/>',
110 my ($name,$join) = @_;
112 die "no data array" unless ($data->{'data'} && ref($data->{'data'}) eq 'ARRAY');
113 # Hm? Should we die here?
114 return unless ($name);
116 my $item = first { $_->{'name'} eq $name } @{ $data->{'data'} };
118 return unless($item);
120 my $v = $item->{$type} || return;
122 if (ref($v) eq 'ARRAY') {
126 $join = $default_delimiter->{$type} unless defined($join);
127 $v = join($join, @{$v});
135 $args->{'d'} = tt_filter_type($args, 'display');
139 $self->{'tt'}->process(
143 ) || $log->logconfess( "apply can't process template: ", $self->{'tt'}->error() );
150 Create output from in-memory data structure using Template Toolkit template
155 template => 'text.tt',
166 my $log = $self->_get_logger();
168 my $file = $args->{'file'} || $log->logconfess("need file name");
170 $log->debug("creating file ",$file);
172 open(my $fh, ">", $file) || $log->logdie("can't open output file '$file': $!");
173 print $fh $self->output(
174 template => $args->{'template'},
175 data => $args->{'data'},
176 ) || $log->logdie("print: $!");
177 close($fh) || $log->logdie("close: $!");
185 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
187 =head1 COPYRIGHT & LICENSE
189 Copyright 2005 Dobrica Pavlinusic, All Rights Reserved.
191 This program is free software; you can redistribute it and/or modify it
192 under the same terms as Perl itself.
196 1; # End of WebPAC::Output::TT