should be if not unless
[catagits/Catalyst-View-TT.git] / lib / Catalyst / View / TT.pm
CommitLineData
8077080c 1package Catalyst::View::TT;
2
3use strict;
caa61517 4use base qw/Catalyst::Base/;
8077080c 5use Template;
6use Template::Timer;
7use NEXT;
8
caa61517 9our $VERSION = '0.07';
8077080c 10
11__PACKAGE__->mk_accessors('template');
8077080c 12
13=head1 NAME
14
15Catalyst::View::TT - Template View Class
16
17=head1 SYNOPSIS
18
19 # use the helper
caa61517 20 create.pl view TT TT
8077080c 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
7b592fc7 35This is the C<Template> view class. Your subclass should inherit
36from this class. If you want to override TT config settings, you
37can do it there by setting __PACKAGE__->config->{OPTION} as shown
38in the synopsis. Of interest might be EVAL_PERL, which is disabled
39by default, and LOAD_TEMPLATES, which is set to use the provider.
40
41If you want to use EVAL perl, add something like this:
42
43 __PACKAGE__->config->{EVAL_PERL} = 1;
44 __PACKAGE__->config->{LOAD_TEMPLATES} = undef;
8077080c 45
caa61517 46=head2 METHODS
8077080c 47
48=cut
49
50sub new {
caa61517 51 my $self = shift;
52 my $c = shift;
53 $self = $self->NEXT::new(@_);
8077080c 54 my $root = $c->config->{root};
caa61517 55 my %config = (
56 EVAL_PERL => 0,
57 INCLUDE_PATH => [ $root, "$root/base" ],
4e705c40 58 %{ $self->config() }
caa61517 59 );
62728755 60
c1c75b07 61 if ( $c->debug && not exists $config{CONTEXT} ) {
62728755 62 $config{CONTEXT} = Template::Timer->new(%config);
63 }
64
caa61517 65 $self->template( Template->new( \%config ) );
8077080c 66 return $self;
67}
68
69=head3 process
70
71Renders the template specified in $c->stash->{template} or $c->request->match
72to $c->response->output.
73
74=cut
75
76sub 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
110This allows your view subclass to pass additional settings to the
111TT config hash.
112
113=cut
114
115=head1 SEE ALSO
116
117L<Catalyst>.
118
119=head1 AUTHOR
120
121Sebastian Riedel, C<sri@cpan.org>
d938377b 122Marcus Ramberg, C<mramberg@cpan.org>
8077080c 123
124=head1 COPYRIGHT
125
126This program is free software, you can redistribute it and/or modify it under
127the same terms as Perl itself.
128
129=cut
130
1311;