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',
83 my $log = $self->_get_logger();
85 foreach my $a (qw/template data/) {
86 $log->logconfess("need $a") unless ($args->{$a});
89 # filter to return value from @ds
93 my ($name,$join) = @_;
95 print "### name = ",Dumper($name);
97 my $item = first { $_->{'name'} eq $name } @{ $args->{'data'} };
101 print "### item = ",Dumper($item);
103 my $v = $item->{'display'} || return;
105 if (ref($v) eq 'ARRAY') {
109 $join = '¶<br/>' unless defined($join);
110 $v = join($join, @{$v});
113 print "### v = $v\n";
116 $args->{'d'} = \&tt_filter_d;
121 $self->{'tt'}->process(
125 ) || $log->logconfess( "apply can't process template: ", $self->{'tt'}->error() );
132 Create output from in-memory data structure using Template Toolkit template
137 template => 'text.tt',
148 my $log = $self->_get_logger();
150 my $file = $args->{'file'} || $log->logconfess("need file name");
152 $log->debug("creating file ",$file);
154 open(my $fh, ">", $file) || $log->logdie("can't open output file '$file': $!");
155 print $fh $self->output(
156 template => $args->{'template'},
157 data => $args->{'data'},
158 ) || $log->logdie("print: $!");
159 close($fh) || $log->logdie("close: $!");
167 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
169 =head1 COPYRIGHT & LICENSE
171 Copyright 2005 Dobrica Pavlinusic, All Rights Reserved.
173 This program is free software; you can redistribute it and/or modify it
174 under the same terms as Perl itself.
178 1; # End of WebPAC::Output::TT