package Excel::Template::Plus::TT; use Moose; use Moose::Util::TypeConstraints; use Template (); use IO::String (); use Excel::Template; our $VERSION = '0.04'; our $AUTHORITY = 'cpan:STEVAN'; with 'MooseX::Param'; subtype 'IO::Handle' => as 'Object' => where { $_->isa('IO::Handle') }; has 'template' => ( is => 'ro', # can either be a: # - filename # - scalar ref to string # - open filehandle # - an IO::Handle instance # (basically anything TT takes) isa => 'Str | ScalarRef | FileHandle | IO::Handle', required => 1, ); has 'config' => ( is => 'ro', isa => 'HashRef', default => sub {{}}, ); has '_template_object' => ( is => "rw", isa => "Template", lazy => 1, default => sub { my $self = shift; my $class = $self->template_class; Class::MOP::load_class($class); ($class->isa('Template')) || confess "The template_class must be Template or a subclass of it"; $class->new( $self->config ) } ); has 'template_class' => ( is => "rw", isa => "Str", default => "Template", ); ## private attributes has '_excel_template' => ( is => 'ro', isa => 'Excel::Template', handles => [qw[ output write_file ]], lazy => 1, default => sub { my $self = shift; $self->_prepare_excel_template; } ); sub _prepare_excel_template { my $self = shift; my $buf; my $fh = IO::String->new(\$buf); my $tt = $self->_template_object; $tt->process( $self->template, $self->params, $fh, ); $fh->pos(0); confess "Template creation failed because : " . $tt->error() if $tt->error(); confess "Template failed to produce any output" unless length($buf); my $excel_template = eval { Excel::Template->new(file => $fh) }; if ($@) { warn $buf; confess $@; } return $excel_template; } no Moose; no Moose::Util::TypeConstraints; 1; __END__ =pod =head1 NAME Excel::Template::Plus::TT - Extension of Excel::Template to use TT =head1 SYNOPSIS use Excel::Template::Plus::TT; # this is most commonly used through # the Excel::Template::Plus factory my $template = Excel::Template::Plus::TT->new( template => 'greeting.tmpl', config => { INCLUDE => [ '/templates' ] }, params => { greeting => 'Hello' } ); $template->param(location => 'World'); $template->write_file('greeting.xls'); =head1 DESCRIPTION This is an engine for Excel::Template::Plus which replaces the standard Excel::Template template features with TT. See the L docs for more information. =head1 METHODS =head2 Accessors =over 4 =item B =item B