Commit | Line | Data |
8077080c |
1 | package Catalyst::View::TT; |
2 | |
3 | use strict; |
caa61517 |
4 | use base qw/Catalyst::Base/; |
8077080c |
5 | use Template; |
6 | use Template::Timer; |
7 | use NEXT; |
8 | |
9300af5b |
9 | our $VERSION = '0.13'; |
8077080c |
10 | |
11 | __PACKAGE__->mk_accessors('template'); |
8077080c |
12 | |
13 | =head1 NAME |
14 | |
15 | Catalyst::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 |
42 | This is the Catalyst view class for the L<Template |
43 | Toolkit|Template>. Your application subclass should inherit from this |
44 | class. This plugin renders the template specified in |
45 | C<$c-E<gt>stash-E<gt>{template}>, or failing that, |
46 | C<$c-E<gt>request-E<gt>match>. The template variables are set up from |
47 | the contents of C<$c-E<gt>stash>, augmented with template variable |
48 | C<base> set to Catalyst's C<$c-E<gt>req-E<gt>base>, template variable |
49 | C<c> to Catalyst's C<$c>, and template variable C<name> to Catalyst's |
50 | C<$c-E<gt>config-E<gt>{name}>. The output is stored in |
51 | C<$c-E<gt>response-E<gt>output>. |
52 | |
53 | If you want to override TT config settings, you can do it in your |
54 | application's view class by setting |
55 | C<__PACKAGE__-E<gt>config-E<gt>{OPTION}>, as shown in the Synopsis. Of |
56 | interest might be C<EVAL_PERL>, which is disabled by default, |
57 | C<INCLUDE_PATH>, and C<LOAD_TEMPLATES>, which is set to use the |
58 | provider. |
59 | |
60 | If 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 |
65 | If you have configured Catalyst for debug output, C<Catalyst::View::TT> |
66 | will enable profiling of template processing (using |
67 | L<Template::Timer>). This will embed HTML comments in the output from |
68 | your 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 |
80 | You 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 | |
91 | The constructor for the TT view. Sets up the template provider, |
92 | and reads the application config. |
93 | |
8077080c |
94 | =cut |
95 | |
96 | sub new { |
9300af5b |
97 | my ( $class, $c, $arguments ) = @_; |
98 | |
99 | my $root = $c->config->{root}; |
100 | |
caa61517 |
101 | my %config = ( |
102 | EVAL_PERL => 0, |
103 | INCLUDE_PATH => [ $root, "$root/base" ], |
9300af5b |
104 | %{ $class->config }, |
105 | %{ $arguments } |
caa61517 |
106 | ); |
62728755 |
107 | |
c1c75b07 |
108 | if ( $c->debug && not exists $config{CONTEXT} ) { |
62728755 |
109 | $config{CONTEXT} = Template::Timer->new(%config); |
110 | } |
111 | |
9300af5b |
112 | return $class->NEXT::new( $c, { template => Template->new( \%config ) } ); |
8077080c |
113 | } |
114 | |
2774dc77 |
115 | =item process |
8077080c |
116 | |
722fede4 |
117 | Renders the template specified in C<$c-E<gt>stash-E<gt>{template}> or |
118 | C<$c-E<gt>request-E<gt>match>. Template variables are set up from the |
119 | contents of C<$c-E<gt>stash>, augmented with C<base> set to |
120 | C<$c-E<gt>req-E<gt>base>, C<c> to C<$c> and C<name> to |
121 | C<$c-E<gt>config-E<gt>{name}>. Output is stored in |
122 | C<$c-E<gt>response-E<gt>output>. |
8077080c |
123 | |
124 | =cut |
125 | |
126 | sub 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 | |
171 | This allows your view subclass to pass additional settings to the |
172 | TT config hash. |
173 | |
2774dc77 |
174 | =back |
8077080c |
175 | |
176 | =head1 SEE ALSO |
177 | |
722fede4 |
178 | L<Catalyst>, L<Template::Manual> |
8077080c |
179 | |
180 | =head1 AUTHOR |
181 | |
182 | Sebastian Riedel, C<sri@cpan.org> |
d938377b |
183 | Marcus Ramberg, C<mramberg@cpan.org> |
722fede4 |
184 | Jesse Sheidlower, C<jester@panix.com> |
8077080c |
185 | |
186 | =head1 COPYRIGHT |
187 | |
2774dc77 |
188 | This program is free software, you can redistribute it and/or modify it |
189 | under the same terms as Perl itself. |
8077080c |
190 | |
191 | =cut |
192 | |
193 | 1; |