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