--- trunk/lib/WebPAC/Output/html.pm 2005/06/25 20:23:23 1 +++ trunk/lib/WebPAC/Output/TT.pm 2005/07/24 15:35:46 33 @@ -1,11 +1,16 @@ -package WebPAC::Output::html; +package WebPAC::Output::TT; use warnings; use strict; +use base qw/WebPAC::Common/; + +use Template; +use Data::Dumper; + =head1 NAME -WebPAC::Output::html - The great new WebPAC::Output::html! +WebPAC::Output::TT - use Template Toolkit to produce output =head1 VERSION @@ -17,49 +22,118 @@ =head1 SYNOPSIS -Quick summary of what the module does. +Produce output using Template Toolkit. -Perhaps a little code snippet. +=head1 FUNCTIONS - use WebPAC::Output::html; +=head2 new - my $foo = WebPAC::Output::html->new(); - ... +Create new instance. -=head1 EXPORT + my $tt = new WebPAC::Output::TT( + include_path => '/path/to/conf/output/tt', + filters => { + filter_1 => sub { uc(shift) }, + }, + ); -A list of functions that can be exported. You can delete this section -if you don't export anything, such as for a purely object-oriented module. +By default, Template Toolkit will C if included in templates. -=head1 FUNCTIONS +=cut -=head2 function1 +sub new { + my $class = shift; + my $self = {@_}; + bless($self, $class); + + my $log = $self->_get_logger; + + # create Template toolkit instance + $self->{'tt'} = Template->new( + INCLUDE_PATH => $self->{'include_path'}, + FILTERS => $self->{'filter'}, + EVAL_PERL => 1, + ); + + $log->logdie("can't create TT object: $Template::ERROR") unless ($self->{'tt'}); -=cut + $log->debug("filters defined: ",Dumper($self->{'filter'})); -sub function1 { + $self ? return $self : return undef; } -=head2 function2 + +=head2 apply + +Create output from in-memory data structure using Template Toolkit template. + + my $text = $tt->apply( + template => 'text.tt', + data => \@ds + ); =cut -sub function2 { +sub apply { + my $self = shift; + + my $args = {@_}; + + my $log = $self->_get_logger(); + + foreach my $a (qw/template data/) { + $log->logconfess("need $a") unless ($args->{$a}); + } + + my $out; + + $self->{'tt'}->process( + $args->{'template'}, + $args, + \$out + ) || $log->logconfess( "apply can't process template: ", $self->{'tt'}->error() ); + + return $out; } -=head1 AUTHOR +=head2 to_file -Dobrica Pavlinusic, C<< >> +Create output from in-memory data structure using Template Toolkit template +to a file. + + $tt->to_file( + file => 'out.txt', + template => 'text.tt', + data => \@ds + ); + +=cut + +sub to_file { + my $self = shift; + + my $args = {@_}; + + my $log = $self->_get_logger(); -=head1 BUGS + my $file = $args->{'file'} || $log->logconfess("need file name"); -Please report any bugs or feature requests to -C, or through the web interface at -L. -I will be notified, and then you'll automatically be notified of progress on -your bug as I make changes. + $log->debug("creating file ",$file); -=head1 ACKNOWLEDGEMENTS + open(my $fh, ">", $file) || $log->logdie("can't open output file '$file': $!"); + print $fh $self->output( + template => $args->{'template'}, + data => $args->{'data'}, + ) || $log->logdie("print: $!"); + close($fh) || $log->logdie("close: $!"); + + return 1; +} + + +=head1 AUTHOR + +Dobrica Pavlinusic, C<< >> =head1 COPYRIGHT & LICENSE @@ -70,4 +144,4 @@ =cut -1; # End of WebPAC::Output::html +1; # End of WebPAC::Output::TT