updated and fixed C::View::TT docs
[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
23042c3c 9our $VERSION = '0.12';
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
722fede4 20 myapp_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
722fede4 29 # in practice you'd probably set this from a config file;
30 # defaults to $c->config->root
31 __PACKAGE__->config->{INCLUDE_PATH} =
32 '/usr/local/generic/templates:/usr/local/myapp/templates';
33
8077080c 34 1;
35
722fede4 36 # Meanwhile, maybe in a private C<end> action
8077080c 37 $c->forward('MyApp::View::TT');
38
4687ac0d 39
8077080c 40=head1 DESCRIPTION
41
722fede4 42This is the Catalyst view class for the L<Template
43Toolkit|Template>. Your application subclass should inherit from this
44class. This plugin renders the template specified in
45C<$c-E<gt>stash-E<gt>{template}>, or failing that,
46C<$c-E<gt>request-E<gt>match>. The template variables are set up from
47the contents of C<$c-E<gt>stash>, augmented with template variable
48C<base> set to Catalyst's C<$c-E<gt>req-E<gt>base>, template variable
49C<c> to Catalyst's C<$c>, and template variable C<name> to Catalyst's
50C<$c-E<gt>config-E<gt>{name}>. The output is stored in
51C<$c-E<gt>response-E<gt>output>.
52
53If you want to override TT config settings, you can do it in your
54application's view class by setting
55C<__PACKAGE__-E<gt>config-E<gt>{OPTION}>, as shown in the Synopsis. Of
56interest might be C<EVAL_PERL>, which is disabled by default,
57C<INCLUDE_PATH>, and C<LOAD_TEMPLATES>, which is set to use the
58provider.
59
60If you want to use C<EVAL_PERL>, add something like this:
7b592fc7 61
62 __PACKAGE__->config->{EVAL_PERL} = 1;
63 __PACKAGE__->config->{LOAD_TEMPLATES} = undef;
8077080c 64
722fede4 65If you have configured Catalyst for debug output, C<Catalyst::View::TT>
66will enable profiling of template processing (using
67L<Template::Timer>). This will embed HTML comments in the output from
68your templates, such as:
4687ac0d 69
70 <!-- TIMER START: process mainmenu/mainmenu.ttml -->
71 <!-- TIMER START: include mainmenu/cssindex.tt -->
72 <!-- TIMER START: process mainmenu/cssindex.tt -->
73 <!-- TIMER END: process mainmenu/cssindex.tt (0.017279 seconds) -->
74 <!-- TIMER END: include mainmenu/cssindex.tt (0.017401 seconds) -->
75
76 ....
77
78 <!-- TIMER END: process mainmenu/footer.tt (0.003016 seconds) -->
79
722fede4 80You can suppress template profiling when debug is enabled by setting:
4687ac0d 81
82 __PACKAGE__->config->{CONTEXT} = undef;
83
84
caa61517 85=head2 METHODS
8077080c 86
2774dc77 87=over 4
88
89=item new
90
91The constructor for the TT view. Sets up the template provider,
92and reads the application config.
93
8077080c 94=cut
95
96sub new {
caa61517 97 my $self = shift;
98 my $c = shift;
99 $self = $self->NEXT::new(@_);
8077080c 100 my $root = $c->config->{root};
caa61517 101 my %config = (
102 EVAL_PERL => 0,
103 INCLUDE_PATH => [ $root, "$root/base" ],
4e705c40 104 %{ $self->config() }
caa61517 105 );
62728755 106
c1c75b07 107 if ( $c->debug && not exists $config{CONTEXT} ) {
62728755 108 $config{CONTEXT} = Template::Timer->new(%config);
109 }
110
caa61517 111 $self->template( Template->new( \%config ) );
8077080c 112 return $self;
113}
114
2774dc77 115=item process
8077080c 116
722fede4 117Renders the template specified in C<$c-E<gt>stash-E<gt>{template}> or
118C<$c-E<gt>request-E<gt>match>. Template variables are set up from the
119contents of C<$c-E<gt>stash>, augmented with C<base> set to
120C<$c-E<gt>req-E<gt>base>, C<c> to C<$c> and C<name> to
121C<$c-E<gt>config-E<gt>{name}>. Output is stored in
122C<$c-E<gt>response-E<gt>output>.
8077080c 123
124=cut
125
126sub process {
127 my ( $self, $c ) = @_;
23042c3c 128
129 my $template = $c->stash->{template} || $c->request->match;
130
131 unless ($template) {
8077080c 132 $c->log->debug('No template specified for rendering') if $c->debug;
133 return 0;
134 }
23042c3c 135
136 $c->log->debug(qq/Rendering template "$template"/) if $c->debug;
137
138 my $output;
139
8077080c 140 unless (
141 $self->template->process(
23042c3c 142 $template,
8077080c 143 {
8077080c 144 base => $c->req->base,
145 c => $c,
57cfe226 146 name => $c->config->{name},
147 %{ $c->stash }
8077080c 148 },
149 \$output
150 )
151 )
152 {
153 my $error = $self->template->error;
154 $error = qq/Couldn't render template "$error"/;
155 $c->log->error($error);
becb7ac2 156 $c->error($error);
23042c3c 157 return 0;
158 }
159
160 unless ( $c->response->content_type ) {
161 $c->response->content_type('text/html; charset=utf-8');
8077080c 162 }
23042c3c 163
164 $c->response->body($output);
165
8077080c 166 return 1;
167}
168
2774dc77 169=item config
8077080c 170
171This allows your view subclass to pass additional settings to the
172TT config hash.
173
2774dc77 174=back
8077080c 175
176=head1 SEE ALSO
177
722fede4 178L<Catalyst>, L<Template::Manual>
8077080c 179
180=head1 AUTHOR
181
182Sebastian Riedel, C<sri@cpan.org>
d938377b 183Marcus Ramberg, C<mramberg@cpan.org>
722fede4 184Jesse Sheidlower, C<jester@panix.com>
8077080c 185
186=head1 COPYRIGHT
187
2774dc77 188This program is free software, you can redistribute it and/or modify it
189under the same terms as Perl itself.
8077080c 190
191=cut
192
1931;