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