1 package Catalyst::View::TT;
4 use base qw/Catalyst::Base/;
11 __PACKAGE__->mk_accessors('template');
15 Catalyst::View::TT - Template View Class
22 # lib/MyApp/View/TT.pm
23 package MyApp::View::TT;
25 use base 'Catalyst::View::TT';
27 __PACKAGE__->config->{DEBUG} = 'all';
31 # Meanwhile, maybe in an '!end' action
32 $c->forward('MyApp::View::TT');
37 This is the C<Template> view class. Your subclass should inherit from this
38 class. The plugin renders the template specified in C<< $c->stash->{template} >>
39 or C<< $c->request->match >>. The template variables are set up from the
40 contents of C<< $c->stash >>, augmented with C<base> set to C<< $c->req->base >>,
41 C<c> to C<$c> and C<name> to C<< $c->config->{name} >>. The output is
42 stored in C<< $c->response->output >>.
45 If you want to override TT config settings, you can do it there by setting
46 C<< __PACKAGE__->config->{OPTION} >> as shown in the synopsis. Of interest might be
47 C<EVAL_PERL>, which is disabled by default, and C<LOAD_TEMPLATES>, which is set to
50 If you want to use EVAL perl, add something like this:
52 __PACKAGE__->config->{EVAL_PERL} = 1;
53 __PACKAGE__->config->{LOAD_TEMPLATES} = undef;
55 If you have configured Catalyst for debug output C<Catalyst::View::TT> will
56 enable profiling of template processing (using C<Template::Timer>. This will cause
57 HTML comments will get embedded in the output from your templates, such as:
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) -->
67 <!-- TIMER END: process mainmenu/footer.tt (0.003016 seconds) -->
69 You can supress template profiling when debug is enabled by setting:
71 __PACKAGE__->config->{CONTEXT} = undef;
80 The constructor for the TT view. Sets up the template provider,
81 and reads the application config.
88 $self = $self->NEXT::new(@_);
89 my $root = $c->config->{root};
92 INCLUDE_PATH => [ $root, "$root/base" ],
96 if ( $c->debug && not exists $config{CONTEXT} ) {
97 $config{CONTEXT} = Template::Timer->new(%config);
100 $self->template( Template->new( \%config ) );
106 Renders the template specified in C<< $c->stash->{template} >> or C<<
107 $c->request->match >>.
108 Template variables are set up from the contents of C<< $c->stash >>,
109 Jaugmented with C<base> set to C<< $c->req->base >>, C<c> to C<$c> and
110 C<name> to C<< $c->config->{name} >>. Output is stored in
111 C<< $c->response->output >>.
116 my ( $self, $c ) = @_;
117 $c->res->headers->content_type('text/html; charset=utf-8')
118 unless $c->res->headers->content_type();
120 my $name = $c->stash->{template} || $c->req->match;
122 $c->log->debug('No template specified for rendering') if $c->debug;
125 $c->log->debug(qq/Rendering template "$name"/) if $c->debug;
127 $self->template->process(
130 base => $c->req->base,
132 name => $c->config->{name},
139 my $error = $self->template->error;
140 $error = qq/Couldn't render template "$error"/;
141 $c->log->error($error);
144 $c->res->output($output);
150 This allows your view subclass to pass additional settings to the
157 L<Catalyst>. L<Template::Manual>
161 Sebastian Riedel, C<sri@cpan.org>
162 Marcus Ramberg, C<mramberg@cpan.org>
166 This program is free software, you can redistribute it and/or modify it
167 under the same terms as Perl itself.