new helper api
[catagits/Catalyst-View-TT.git] / lib / Catalyst / View / TT.pm
CommitLineData
8077080c 1package Catalyst::View::TT;
2
3use strict;
4use base qw/Catalyst::Base Class::Data::Inheritable/;
5use Template;
6use Template::Timer;
7use NEXT;
8
6bf01217 9our $VERSION = '0.06';
8077080c 10
11__PACKAGE__->mk_accessors('template');
12__PACKAGE__->mk_classdata('config');
13
7b592fc7 14__PACKAGE__->config( { EVAL_PERL => 0 } );
8077080c 15
16=head1 NAME
17
18Catalyst::View::TT - Template View Class
19
20=head1 SYNOPSIS
21
22 # use the helper
23 create view TT TT
24
25 # lib/MyApp/View/TT.pm
26 package MyApp::View::TT;
27
28 use base 'Catalyst::View::TT';
29
30 __PACKAGE__->config->{DEBUG} = 'all';
31
32 1;
33
34 $c->forward('MyApp::View::TT');
35
36=head1 DESCRIPTION
37
7b592fc7 38This is the C<Template> view class. Your subclass should inherit
39from this class. If you want to override TT config settings, you
40can do it there by setting __PACKAGE__->config->{OPTION} as shown
41in the synopsis. Of interest might be EVAL_PERL, which is disabled
42by default, and LOAD_TEMPLATES, which is set to use the provider.
43
44If you want to use EVAL perl, add something like this:
45
46 __PACKAGE__->config->{EVAL_PERL} = 1;
47 __PACKAGE__->config->{LOAD_TEMPLATES} = undef;
8077080c 48
49=head2 OVERLOADED METHODS
50
51=cut
52
53sub new {
54 my $class = shift;
55 my $c = shift;
56 my $self = $class->NEXT::new(@_);
7b592fc7 57 our ($template, $provider);
8077080c 58 my $root = $c->config->{root};
7b592fc7 59 $provider ||= Template::Provider->new();
60 $provider->include_path([ $root, "$root/base" ]);
61 my %config= ( LOAD_TEMPLATES => [ $provider ],
62 %{ $class->config() },
63 INCLUDE_PATH => [ $root, "$root/base" ]
64 );
8077080c 65 $config{CONTEXT} = Template::Timer->new(%config) if $c->debug;
7b592fc7 66 $self->template( Template->new(\%config));
8077080c 67 return $self;
68}
69
70=head3 process
71
72Renders the template specified in $c->stash->{template} or $c->request->match
73to $c->response->output.
74
75=cut
76
77sub process {
78 my ( $self, $c ) = @_;
79 $c->res->headers->content_type('text/html;charset=utf8');
80 my $output;
81 my $name = $c->stash->{template} || $c->req->match;
82 unless ($name) {
83 $c->log->debug('No template specified for rendering') if $c->debug;
84 return 0;
85 }
86 $c->log->debug(qq/Rendering template "$name"/) if $c->debug;
87 unless (
88 $self->template->process(
89 $name,
90 {
91 %{ $c->stash },
92 base => $c->req->base,
93 c => $c,
94 name => $c->config->{name}
95 },
96 \$output
97 )
98 )
99 {
100 my $error = $self->template->error;
101 $error = qq/Couldn't render template "$error"/;
102 $c->log->error($error);
103 $c->errors($error);
104 }
105 $c->res->output($output);
106 return 1;
107}
108
109=head3 config
110
111This allows your view subclass to pass additional settings to the
112TT config hash.
113
114=cut
115
116=head1 SEE ALSO
117
118L<Catalyst>.
119
120=head1 AUTHOR
121
122Sebastian Riedel, C<sri@cpan.org>
123Marcus Ramberg
124
125=head1 COPYRIGHT
126
127This program is free software, you can redistribute it and/or modify it under
128the same terms as Perl itself.
129
130=cut
131
1321;