-
Notifications
You must be signed in to change notification settings - Fork 0
/
Any.pm
108 lines (79 loc) · 2.38 KB
/
Any.pm
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
use strict;
use warnings;
package CGI::Application::Plugin::TT::Any;
# ABSTRACT: load CGI::Application::Plugin::TT with any TT-compatible class
=head1 SYNOPSIS
use strict;
use warnings;
package MyCGIApp;
use base qw(CGI::Application);
use CGI::Application::Plugin::TT;
use CGI::Application::Plugin::TT::Any;
sub setup {
my ( $self ) = @_;
$self->tt_config(
TEMPLATE_OPTIONS => {
CLASS => 'Template::AutoFilter',
INCLUDE_PATH => 't',
},
);
}
sub myrunmode {
my ( $self ) = @_;
my %params = ( email => 'email@company.com' );
return $self->tt_process( 'template.tmpl', \%params );
}
Alternatively:
use strict;
use warnings;
package MyCGIApp;
use base qw(CGI::Application);
use CGI::Application::Plugin::TT;
use CGI::Application::Plugin::TT::Any (
TEMPLATE_OPTIONS => {
CLASS => 'Template::AutoFilter',
INCLUDE_PATH => 't',
},
);
sub myrunmode {
my ( $self ) = @_;
my %params = ( email => 'email@company.com' );
return $self->tt_process( 'template.tmpl', \%params );
}
=head1 METHODS
=head2 tt_obj
Overrides L<CGI::Application::Plugin::TT>'s tt_obj() with a version that
inspects the CLASS field of the TEMPLATE_OPTIONS hashref in the options
and, if set, loads the Template object using that class. Otherwise it
defaults to Template.
=cut
use Class::Load 'load_class';
use Carp;
require CGI::Application::Plugin::TT;
sub import {
my $pkg = shift;
my $callpkg = caller;
{
no warnings 'redefine';
no strict 'refs';
for (qw( tt_obj )) {
my %target = %{"$callpkg\::"};
die "CGI::Application::Plugin::TT needs to be loaded first" if !$target{$_};
*{"$callpkg\::$_"} = \&{$_};
}
}
return if !@_;
return $callpkg->tt_config( @_ );
}
sub tt_obj {
my $self = shift;
my ($tt, $options, $frompkg) = CGI::Application::Plugin::TT::_get_object_or_options($self);
return $tt if $tt;
my $tt_options = $options->{TEMPLATE_OPTIONS} || {};
my $class = delete $tt_options->{CLASS} || 'Template';
load_class $class;
$tt = $class->new( $tt_options ) || carp "Can't load Template";
CGI::Application::Plugin::TT::_set_object( $frompkg||$self, $tt );
return $tt;
}
1;