File: TT.pm

package info (click to toggle)
libexcel-template-plus-perl 0.06-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 116 kB
  • sloc: perl: 200; makefile: 2
file content (214 lines) | stat: -r--r--r-- 4,019 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214

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

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

use Excel::Template;

our $VERSION   = '0.06';
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;
        Module::Runtime::use_module($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<Excel::Template::Plus> docs for more information.

=head1 METHODS

=head2 Accessors

=over 4

=item B<config>

=item B<template>

=item B<template_class>

=item B<params>

=back

=head2 Excel::Template compat methods

=over 4

=item B<params ($name | $name => $value)>

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

=item B<output>

Returns the generated excel file.

=item B<write_file ($filename)>

Writes the generated excel file to C<$filename>.

=back

=head2 Housekeeping

=over 4

=item B<DEMOLISH>

This will cleanup any temp files generated in the process.

=item B<meta>

Returns the metaclass.

=back

=head1 BUGS

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.

=head1 ACKNOWLEDGEMENTS

=over 4

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

=back

=head1 AUTHOR

Stevan Little E<lt>stevan@cpan.orgE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright 2007-2014 by Infinity Interactive, Inc.

L<http://www.iinteractive.com>

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself. 

=cut