should be if not unless
[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
61     if ( $c->debug && not exists $config{CONTEXT} ) {
62        $config{CONTEXT} = Template::Timer->new(%config);
63     }
64
65     $self->template( Template->new( \%config ) );
66     return $self;
67 }
68
69 =head3 process
70
71 Renders the template specified in $c->stash->{template} or $c->request->match
72 to $c->response->output.
73
74 =cut
75
76 sub process {
77     my ( $self, $c ) = @_;
78     $c->res->headers->content_type('text/html;charset=utf8');
79     my $output;
80     my $name = $c->stash->{template} || $c->req->match;
81     unless ($name) {
82         $c->log->debug('No template specified for rendering') if $c->debug;
83         return 0;
84     }
85     $c->log->debug(qq/Rendering template "$name"/) if $c->debug;
86     unless (
87         $self->template->process(
88             $name,
89             {
90                 %{ $c->stash },
91                 base => $c->req->base,
92                 c    => $c,
93                 name => $c->config->{name}
94             },
95             \$output
96         )
97       )
98     {
99         my $error = $self->template->error;
100         $error = qq/Couldn't render template "$error"/;
101         $c->log->error($error);
102         $c->errors($error);
103     }
104     $c->res->output($output);
105     return 1;
106 }
107
108 =head3 config
109
110 This allows your view subclass to pass additional settings to the
111 TT config hash.
112
113 =cut 
114
115 =head1 SEE ALSO
116
117 L<Catalyst>.
118
119 =head1 AUTHOR
120
121 Sebastian Riedel, C<sri@cpan.org>
122 Marcus Ramberg, C<mramberg@cpan.org>
123
124 =head1 COPYRIGHT
125
126 This program is free software, you can redistribute it and/or modify it under
127 the same terms as Perl itself.
128
129 =cut
130
131 1;