initial import of catalyst.
[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 Class::Data::Inheritable/;
5 use Template;
6 use Template::Timer;
7 use NEXT;
8
9 our $VERSION = '0.05';
10
11 __PACKAGE__->mk_accessors('template');
12 __PACKAGE__->mk_classdata('config');
13
14 __PACKAGE__->config( { EVAL_PERL => 1 } );
15
16 =head1 NAME
17
18 Catalyst::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
38 This is the C<Template> view class.
39
40 =head2 OVERLOADED METHODS
41
42 =cut
43
44 sub new {
45     my $class  = shift;
46     my $c      = shift;
47     my $self   = $class->NEXT::new(@_);
48     my $root   = $c->config->{root};
49     my %config =
50       ( %{ $class->config }, INCLUDE_PATH => [ $root, "$root/base" ] );
51     $config{CONTEXT} = Template::Timer->new(%config) if $c->debug;
52     $self->template( Template->new( \%config ) );
53     return $self;
54 }
55
56 =head3 process
57
58 Renders the template specified in $c->stash->{template} or $c->request->match
59 to $c->response->output.
60
61 =cut
62
63 sub process {
64     my ( $self, $c ) = @_;
65     $c->res->headers->content_type('text/html;charset=utf8');
66     my $output;
67     my $name = $c->stash->{template} || $c->req->match;
68     unless ($name) {
69         $c->log->debug('No template specified for rendering') if $c->debug;
70         return 0;
71     }
72     $c->log->debug(qq/Rendering template "$name"/) if $c->debug;
73     unless (
74         $self->template->process(
75             $name,
76             {
77                 %{ $c->stash },
78                 base => $c->req->base,
79                 c    => $c,
80                 name => $c->config->{name}
81             },
82             \$output
83         )
84       )
85     {
86         my $error = $self->template->error;
87         $error = qq/Couldn't render template "$error"/;
88         $c->log->error($error);
89         $c->errors($error);
90     }
91     $c->res->output($output);
92     return 1;
93 }
94
95 =head3 config
96
97 This allows your view subclass to pass additional settings to the
98 TT config hash.
99
100 =cut 
101
102 =head1 SEE ALSO
103
104 L<Catalyst>.
105
106 =head1 AUTHOR
107
108 Sebastian Riedel, C<sri@cpan.org>
109 Marcus Ramberg
110
111 =head1 COPYRIGHT
112
113 This program is free software, you can redistribute it and/or modify it under
114 the same terms as Perl itself.
115
116 =cut
117
118 1;