1NAME
2 Catalyst::View::TT - Template View Class
3
4SYNOPSIS
5 # use the helper to create your View
6
7 myapp_create.pl view Web TT
8
9 # add custom configuration in View/Web.pm
10
11 __PACKAGE__->config(
12 # any TT configuration items go here
13 TEMPLATE_EXTENSION => '.tt',
14 CATALYST_VAR => 'c',
15 TIMER => 0,
16 ENCODING => 'utf-8'
17 # Not set by default
18 PRE_PROCESS => 'config/main',
19 WRAPPER => 'site/wrapper',
20 render_die => 1, # Default for new apps, see render method docs
21 expose_methods => [qw/method_in_view_class/],
22 );
23
24 # add include path configuration in MyApp.pm
25
26 __PACKAGE__->config(
27 'View::Web' => {
28 INCLUDE_PATH => [
29 __PACKAGE__->path_to( 'root', 'src' ),
30 __PACKAGE__->path_to( 'root', 'lib' ),
31 ],
32 },
33 );
34
35 # render view from lib/MyApp.pm or
36 lib/MyApp::Controller::SomeController.pm
37
38 sub message : Global {
39 my ( $self, $c ) = @_;
40 $c->stash->{template} = 'message.tt2';
41 $c->stash->{message} = 'Hello World!';
42 $c->forward( $c->view('Web') );
43 }
44
45 # access variables from template
46
47 The message is: [% message %].
48
49 # example when CATALYST_VAR is set to 'Catalyst'
50 Context is [% Catalyst %]
51 The base is [% Catalyst.req.base %]
52 The name is [% Catalyst.config.name %]
53
54 # example when CATALYST_VAR isn't set
55 Context is [% c %]
56 The base is [% base %]
57 The name is [% name %]
58
59DESCRIPTION
60 This is the Catalyst view class for the Template Toolkit. Your
61 application should defined a view class which is a subclass of this
62 module. Throughout this manual it will be assumed that your application
63 is named MyApp and you are creating a TT view named Web; these names are
64 placeholders and should always be replaced with whatever name you've
65 chosen for your application and your view. The easiest way to create a
66 TT view class is through the myapp_create.pl script that is created
67 along with the application:
68
69 $ script/myapp_create.pl view Web TT
70
71 This creates a MyApp::View::Web.pm module in the lib directory (again,
72 replacing "MyApp" with the name of your application) which looks
73 something like this:
74
75 package FooBar::View::Web;
76 use Moose;
77
78 extends 'Catalyst::View::TT';
79
80 __PACKAGE__->config(DEBUG => 'all');
81
82 Now you can modify your action handlers in the main application and/or
83 controllers to forward to your view class. You might choose to do this
84 in the end() method, for example, to automatically forward all actions
85 to the TT view class.
86
87 # In MyApp or MyApp::Controller::SomeController
88
89 sub end : Private {
90 my( $self, $c ) = @_;
91 $c->forward( $c->view('Web') );
92 }
93
94 But if you are using the standard auto-generated end action, you don't
95 even need to do this!
96
97 # in MyApp::Controller::Root
98 sub end : ActionClass('RenderView') {} # no need to change this line
99
100 # in MyApp.pm
101 __PACKAGE__->config(
102 ...
103 default_view => 'Web',
104 );
105
106 This will Just Work. And it has the advantages that:
107
108 * If you want to use a different view for a given request, just set <<
109 $c->stash->{current_view} >>. (See Catalyst's "$c->view" method for
110 details.
111
112 * << $c->res->redirect >> is handled by default. If you just forward
113 to "View::Web" in your "end" routine, you could break this by
114 sending additional content.
115
116 See Catalyst::Action::RenderView for more details.
117
118 CONFIGURATION
119 There are a three different ways to configure your view class. The first
120 way is to call the "config()" method in the view subclass. This happens
121 when the module is first loaded.
122
123 package MyApp::View::Web;
124 use Moose;
125 extends 'Catalyst::View::TT';
126
127 __PACKAGE__->config({
128 PRE_PROCESS => 'config/main',
129 WRAPPER => 'site/wrapper',
130 });
131
132 You may also override the configuration provided in the view class by
133 adding a 'View::Web' section to your application config.
134
135 This should generally be used to inject the include paths into the view
136 to avoid the view trying to load the application to resolve paths.
137
138 .. inside MyApp.pm ..
139 __PACKAGE__->config(
140 'View::Web' => {
141 INCLUDE_PATH => [
142 __PACKAGE__->path_to( 'root', 'templates', 'lib' ),
143 __PACKAGE__->path_to( 'root', 'templates', 'src' ),
144 ],
145 },
146 );
147
148 You can also configure your view from within your config file if you're
149 using Catalyst::Plugin::ConfigLoader. This should be reserved for
150 deployment-specific concerns. For example:
151
152 # MyApp_local.conf (Config::General format)
153
154 <View Web>
155 WRAPPER "custom_wrapper"
156 INCLUDE_PATH __path_to('root/templates/custom_site')__
157 INCLUDE_PATH __path_to('root/templates')__
158 </View>
159
160 might be used as part of a simple way to deploy different instances of
161 the same application with different themes.
162
163 DYNAMIC INCLUDE_PATH
164 Sometimes it is desirable to modify INCLUDE_PATH for your templates at
165 run time.
166
167 Additional paths can be added to the start of INCLUDE_PATH via the stash
168 as follows:
169
170 $c->stash->{additional_template_paths} =
171 [$c->config->{root} . '/test_include_path'];
172
173 If you need to add paths to the end of INCLUDE_PATH, there is also an
174 include_path() accessor available:
175
176 push( @{ $c->view('Web')->include_path }, qw/path/ );
177
178 Note that if you use include_path() to add extra paths to INCLUDE_PATH,
179 you MUST check for duplicate paths. Without such checking, the above
180 code will add "path" to INCLUDE_PATH at every request, causing a memory
181 leak.
182
183 A safer approach is to use include_path() to overwrite the array of
184 paths rather than adding to it. This eliminates both the need to perform
185 duplicate checking and the chance of a memory leak:
186
187 @{ $c->view('Web')->include_path } = qw/path another_path/;
188
189 If you are calling "render" directly then you can specify dynamic paths
190 by having a "additional_template_paths" key with a value of additional
191 directories to search. See "CAPTURING TEMPLATE OUTPUT" for an example
192 showing this.
193
194 Unicode (pre Catalyst v5.90080)
195 NOTE Starting with Catalyst v5.90080 unicode and encoding has been baked
196 into core, and the default encoding is UTF-8. The following advice is
197 for older versions of Catalyst.
198
199 Be sure to set "ENCODING => 'utf-8'" and use
200 Catalyst::Plugin::Unicode::Encoding if you want to use non-ascii
201 characters (encoded as utf-8) in your templates. This is only needed if
202 you actually have UTF8 literals in your templates and the BOM is not
203 properly set. Setting encoding here does not magically encode your
204 template output. If you are using this version of Catalyst you need to
205 all the Unicode plugin, or upgrade (preferred)
206
207 Unicode (Catalyst v5.90080+)
208 This version of Catalyst will automatically encode your body output to
209 UTF8. This means if your variables contain multibyte characters you
210 don't need top do anything else to get UTF8 output. However if your
211 templates contain UTF8 literals (like, multibyte characters actually in
212 the template text), then you do need to either set the BOM mark on the
213 template file or instruct TT to decode the templates at load time via
214 the ENCODING configuration setting. Most of the time you can just do:
215
216 MyApp::View::HTML->config(
217 ENCODING => 'UTF-8');
218
219 and that will just take care of everything. This configuration setting
220 will force Template to decode all files correctly, so that when you hit
221 the finalize_encoding step we can properly encode the body as UTF8. If
222 you fail to do this you will get double encoding issues in your output
223 (but again, only for the UTF8 literals in your actual template text.)
224
225 Again, this ENCODING configuration setting only instructs template
226 toolkit how (and if) to decode the contents of your template files when
227 reading them from disk. It has no other effect.
228
229 RENDERING VIEWS
230 The view plugin renders the template specified in the "template" item in
231 the stash.
232
233 sub message : Global {
234 my ( $self, $c ) = @_;
235 $c->stash->{template} = 'message.tt2';
236 $c->forward( $c->view('Web') );
237 }
238
239 If a stash item isn't defined, then it instead uses the stringification
240 of the action dispatched to (as defined by $c->action) in the above
241 example, this would be "message", but because the default is to append
242 '.tt', it would load "root/message.tt".
243
244 The items defined in the stash are passed to the Template Toolkit for
245 use as template variables.
246
247 sub default : Private {
248 my ( $self, $c ) = @_;
249 $c->stash->{template} = 'message.tt2';
250 $c->stash->{message} = 'Hello World!';
251 $c->forward( $c->view('Web') );
252 }
253
254 A number of other template variables are also added:
255
256 c A reference to the context object, $c
257 base The URL base, from $c->req->base()
258 name The application name, from $c->config->{ name }
259
260 These can be accessed from the template in the usual way:
261
262 <message.tt2>:
263
264 The message is: [% message %]
265 The base is [% base %]
266 The name is [% name %]
267
268 The output generated by the template is stored in "$c->response->body".
269
270 CAPTURING TEMPLATE OUTPUT
271 If you wish to use the output of a template for some other purpose than
272 displaying in the response, e.g. for sending an email, this is possible
273 using other views, such as Catalyst::View::Email::Template.
274
275 TEMPLATE PROFILING
276 See ""TIMER"" property of the "config" method.
277
278METHODS
279 new
280 The constructor for the TT view. Sets up the template provider, and
281 reads the application config.
282
283 process($c)
284 Renders the template specified in "$c->stash->{template}" or
285 "$c->action" (the private name of the matched action). Calls render to
286 perform actual rendering. Output is stored in "$c->response->body".
287
288 It is possible to forward to the process method of a TT view from inside
289 Catalyst like this:
290
291 $c->forward('View::Web');
292
293 N.B. This is usually done automatically by Catalyst::Action::RenderView.
294
295 render($c, $template, \%args)
296 Renders the given template and returns output. Throws a
297 Template::Exception object upon error.
298
299 The template variables are set to %$args if $args is a hashref, or
300 "$c->stash" otherwise. In either case the variables are augmented with
301 "base" set to "$c->req->base", "c" to $c, and "name" to
302 "$c->config->{name}". Alternately, the "CATALYST_VAR" configuration item
303 can be defined to specify the name of a template variable through which
304 the context reference ($c) can be accessed. In this case, the "c",
305 "base", and "name" variables are omitted.
306
307 $template can be anything that Template::process understands how to
308 process, including the name of a template file or a reference to a test
309 string. See Template::process for a full list of supported formats.
310
311 To use the render method outside of your Catalyst app, just pass a undef
312 context. This can be useful for tests, for instance.
313
314 It is possible to forward to the render method of a TT view from inside
315 Catalyst to render page fragments like this:
316
317 my $fragment = $c->forward("View::Web", "render", $template_name, $c->stash->{fragment_data});
318
319 Backwards compatibility note
320 The render method used to just return the Template::Exception object,
321 rather than just throwing it. This is now deprecated and instead the
322 render method will throw an exception for new applications.
323
324 This behaviour can be activated (and is activated in the default
325 skeleton configuration) by using "render_die => 1". If you rely on the
326 legacy behaviour then a warning will be issued.
327
328 To silence this warning, set "render_die => 0", but it is recommended
329 you adjust your code so that it works with "render_die => 1".
330
331 In a future release, "render_die => 1" will become the default if
332 unspecified.
333
334 template_vars
335 Returns a list of keys/values to be used as the catalyst variables in
336 the template.
337
338 config
339 This method allows your view subclass to pass additional settings to the
340 TT configuration hash, or to set the options as below:
341
342 paths
343 The list of paths TT will look for templates in.
344
345 expose_methods
346 The list of methods in your View class which should be made available to
347 the templates.
348
349 For example:
350
351 expose_methods => [qw/uri_for_css/],
352
353 ...
354
355 sub uri_for_css {
356 my ($self, $c, $filename) = @_;
357
358 # additional complexity like checking file exists here
359
360 return $c->uri_for('/static/css/' . $filename);
361 }
362
363 Then in the template:
364
365 [% uri_for_css('home.css') %]
366
367 content_type
368 This lets you override the default content type for the response. If you
369 do not set this and if you do not set the content type in your
370 controllers, the default is "text/html; charset=utf-8".
371
372 Use this if you are creating alternative view responses, such as text or
373 JSON and want a global setting.
374
375 Any content type set in your controllers before calling this view are
376 respected and have priority.
377
378 "CATALYST_VAR"
379 Allows you to change the name of the Catalyst context object. If set, it
380 will also remove the base and name aliases, so you will have access them
381 through <context>.
382
383 For example, if CATALYST_VAR has been set to "Catalyst", a template
384 might contain:
385
386 The base is [% Catalyst.req.base %]
387 The name is [% Catalyst.config.name %]
388
389 "TIMER"
390 If you have configured Catalyst for debug output, and turned on the
391 TIMER setting, "Catalyst::View::TT" will enable profiling of template
392 processing (using Template::Timer). This will embed HTML comments in the
393 output from your templates, such as:
394
395 <!-- TIMER START: process mainmenu/mainmenu.ttml -->
396 <!-- TIMER START: include mainmenu/cssindex.tt -->
397 <!-- TIMER START: process mainmenu/cssindex.tt -->
398 <!-- TIMER END: process mainmenu/cssindex.tt (0.017279 seconds) -->
399 <!-- TIMER END: include mainmenu/cssindex.tt (0.017401 seconds) -->
400
401 ....
402
403 <!-- TIMER END: process mainmenu/footer.tt (0.003016 seconds) -->
404
405 "TEMPLATE_EXTENSION"
406 a suffix to add when looking for templates bases on the "match" method
407 in Catalyst::Request.
408
409 For example:
410
411 package MyApp::Controller::Test;
412 sub test : Local { .. }
413
414 Would by default look for a template in <root>/test/test. If you set
415 TEMPLATE_EXTENSION to '.tt', it will look for <root>/test/test.tt.
416
417 "PROVIDERS"
418 Allows you to specify the template providers that TT will use.
419
420 MyApp->config(
421 name => 'MyApp',
422 root => MyApp->path_to('root'),
423 'View::Web' => {
424 PROVIDERS => [
425 {
426 name => 'DBI',
427 args => {
428 DBI_DSN => 'dbi:DB2:books',
429 DBI_USER=> 'foo'
430 }
431 }, {
432 name => '_file_',
433 args => {}
434 }
435 ]
436 },
437 );
438
439 The 'name' key should correspond to the class name of the provider you
440 want to use. The _file_ name is a special case that represents the
441 default TT file-based provider. By default the name is will be prefixed
442 with 'Template::Provider::'. You can fully qualify the name by using a
443 unary plus:
444
445 name => '+MyApp::Provider::Foo'
446
447 You can also specify the 'copy_config' key as an arrayref, to copy those
448 keys from the general config, into the config for the provider:
449
450 DEFAULT_ENCODING => 'utf-8',
451 PROVIDERS => [
452 {
453 name => 'Encoding',
454 copy_config => [qw(DEFAULT_ENCODING INCLUDE_PATH)]
455 }
456 ]
457
458 This can prove useful when you want to use the additional_template_paths
459 hack in your own provider, or if you need to use
460 Template::Provider::Encoding
461
462 "CLASS"
463 Allows you to specify a custom class to use as the template class
464 instead of Template.
465
466 package MyApp::View::Web;
467 use Moose;
468 extends 'Catalyst::View::TT';
469
470 use Template::AutoFilter;
471
472 __PACKAGE__->config({
473 CLASS => 'Template::AutoFilter',
474 });
475
476 This is useful if you want to use your own subclasses of Template, so
477 you can, for example, prevent XSS by automatically filtering all output
478 through "| html".
479
480 HELPERS
481 The Catalyst::Helper::View::TT and Catalyst::Helper::View::TTSite helper
482 modules are provided to create your view module. There are invoked by
483 the myapp_create.pl script:
484
485 $ script/myapp_create.pl view Web TT
486
487 $ script/myapp_create.pl view Web TTSite
488
489 The Catalyst::Helper::View::TT module creates a basic TT view module.
490 The Catalyst::Helper::View::TTSite module goes a little further. It also
491 creates a default set of templates to get you started. It also
492 configures the view module to locate the templates automatically.
493
494NOTES
495 If you are using the CGI module inside your templates, you will
496 experience that the Catalyst server appears to hang while rendering the
497 web page. This is due to the debug mode of CGI (which is waiting for
498 input in the terminal window). Turning off the debug mode using the
499 "-no_debug" option solves the problem, eg.:
500
501 [% USE CGI('-no_debug') %]
502
503SEE ALSO
504 Catalyst, Catalyst::Helper::View::TT, Catalyst::Helper::View::TTSite,
505 Template::Manual
506
507AUTHORS
508 Sebastian Riedel, "sri@cpan.org"
509
510 Marcus Ramberg, "mramberg@cpan.org"
511
512 Jesse Sheidlower, "jester@panix.com"
513
514 Andy Wardley, "abw@cpan.org"
515
516 Luke Saunders, "luke.saunders@gmail.com"
517
518COPYRIGHT
519 This program is free software. You can redistribute it and/or modify it
520 under the same terms as Perl itself.
521
522