Excel::Template::Plus::TT - Extension of Excel::Template to use TT


Excel-Template-Plus documentation Contained in the Excel-Template-Plus distribution.

Index


Code Index:

NAME

Top

Excel::Template::Plus::TT - Extension of Excel::Template to use TT

SYNOPSIS

Top

  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');

DESCRIPTION

Top

This is an engine for Excel::Template::Plus which replaces the standard Excel::Template template features with TT. See the Excel::Template::Plus docs for more information.

METHODS

Top

Accessors

config
template
template_class
params

Excel::Template compat methods

params ($name | $name = $value)>

This provides access to getting and setting the parameters, it behaves exactly like the standard CGI.pm-style param method.

output

Returns the generated excel file.

write_file ($filename)

Writes the generated excel file to $filename.

Housekeeping

DEMOLISH

This will cleanup any temp files generated in the process.

meta

Returns the metaclass.

BUGS

Top

All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT.

ACKNOWLEDGEMENTS

Top

This module was inspired by Excel::Template::TT.

AUTHOR

Top

Stevan Little <stevan@iinteractive.com>

COPYRIGHT AND LICENSE

Top


Excel-Template-Plus documentation Contained in the Excel-Template-Plus distribution.

package Excel::Template::Plus::TT;
use Moose;
use Moose::Util::TypeConstraints;

use Template   ();
use IO::String ();

use Excel::Template;

our $VERSION   = '0.05';
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__