1Catalyst::View::TT(3) User Contributed Perl DocumentationCatalyst::View::TT(3)
2
3
4

NAME

6       Catalyst::View::TT - Template View Class
7

SYNOPSIS

9       # use the helper to create your View
10
11           myapp_create.pl view TT TT
12
13       # configure in lib/MyApp.pm (Could be set from configfile instead)
14
15           __PACKAGE__->config(
16               name         => 'MyApp',
17               root         => MyApp->path_to('root'),
18               default_view => 'TT',
19               'View::TT' => {
20                   # any TT configurations items go here
21                   INCLUDE_PATH => [
22                     MyApp->path_to( 'root', 'src' ),
23                     MyApp->path_to( 'root', 'lib' ),
24                   ],
25                   TEMPLATE_EXTENSION => '.tt',
26                   CATALYST_VAR => 'c',
27                   TIMER        => 0,
28                   # Not set by default
29                   PRE_PROCESS        => 'config/main',
30                   WRAPPER            => 'site/wrapper',
31                   render_die => 1, # Default for new apps, see render method docs
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('TT') );
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

DESCRIPTION

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.  The easiest way to achieve this is using the myapp_create.pl
63       script (where myapp should be replaced with whatever your application
64       is called).  This script is created as part of the Catalyst setup.
65
66           $ script/myapp_create.pl view TT TT
67
68       This creates a MyApp::View::TT.pm module in the lib directory (again,
69       replacing "MyApp" with the name of your application) which looks
70       something like this:
71
72           package FooBar::View::TT;
73
74           use strict;
75           use warnings;
76
77           use base 'Catalyst::View::TT';
78
79           __PACKAGE__->config(DEBUG => 'all');
80
81       Now you can modify your action handlers in the main application and/or
82       controllers to forward to your view class.  You might choose to do this
83       in the end() method, for example, to automatically forward all actions
84       to the TT view class.
85
86           # In MyApp or MyApp::Controller::SomeController
87
88           sub end : Private {
89               my( $self, $c ) = @_;
90               $c->forward( $c->view('TT') );
91           }
92
93       But if you are using the standard auto-generated end action, you don't
94       even need to do this!
95
96           # in MyApp::Controller::Root
97           sub end : ActionClass('RenderView') {} # no need to change this line
98
99           # in MyApp.pm
100           __PACKAGE__->config(
101               ...
102               default_view => 'TT',
103           );
104
105       This will Just Work.  And it has the advantages that:
106
107       ·   If you want to use a different view for a given request, just set
108           << $c->stash->{current_view} >>.  (See Catalyst's "$c->view" method
109           for details.
110
111       ·   << $c->res->redirect >> is handled by default.  If you just forward
112           to "View::TT" in your "end" routine, you could break this by
113           sending additional content.
114
115       See Catalyst::Action::RenderView for more details.
116
117   CONFIGURATION
118       There are a three different ways to configure your view class.  The
119       first way is to call the "config()" method in the view subclass.  This
120       happens when the module is first loaded.
121
122           package MyApp::View::TT;
123
124           use strict;
125           use base 'Catalyst::View::TT';
126
127           MyApp::View::TT->config({
128               INCLUDE_PATH => [
129                   MyApp->path_to( 'root', 'templates', 'lib' ),
130                   MyApp->path_to( 'root', 'templates', 'src' ),
131               ],
132               PRE_PROCESS  => 'config/main',
133               WRAPPER      => 'site/wrapper',
134           });
135
136       The second way is to define a "new()" method in your view subclass.
137       This performs the configuration when the view object is created,
138       shortly after being loaded.  Remember to delegate to the base class
139       "new()" method (via "$self->next::method()" in the example below) after
140       performing any configuration.
141
142           sub new {
143               my $self = shift;
144               $self->config({
145                   INCLUDE_PATH => [
146                       MyApp->path_to( 'root', 'templates', 'lib' ),
147                       MyApp->path_to( 'root', 'templates', 'src' ),
148                   ],
149                   PRE_PROCESS  => 'config/main',
150                   WRAPPER      => 'site/wrapper',
151               });
152               return $self->next::method(@_);
153           }
154
155       The final, and perhaps most direct way, is to define a class item in
156       your main application configuration, again by calling the ubiquitous
157       "config()" method.  The items in the class hash are added to those
158       already defined by the above two methods.  This happens in the base
159       class new() method (which is one reason why you must remember to call
160       it via "MRO::Compat" if you redefine the "new()" method in a subclass).
161
162           package MyApp;
163
164           use strict;
165           use Catalyst;
166
167           MyApp->config({
168               name     => 'MyApp',
169               root     => MyApp->path_to('root'),
170               'View::TT' => {
171                   INCLUDE_PATH => [
172                       MyApp->path_to( 'root', 'templates', 'lib' ),
173                       MyApp->path_to( 'root', 'templates', 'src' ),
174                   ],
175                   PRE_PROCESS  => 'config/main',
176                   WRAPPER      => 'site/wrapper',
177               },
178           });
179
180       Note that any configuration items defined by one of the earlier methods
181       will be overwritten by items of the same name provided by the latter
182       methods.
183
184   DYNAMIC INCLUDE_PATH
185       Sometimes it is desirable to modify INCLUDE_PATH for your templates at
186       run time.
187
188       Additional paths can be added to the start of INCLUDE_PATH via the
189       stash as follows:
190
191           $c->stash->{additional_template_paths} =
192               [$c->config->{root} . '/test_include_path'];
193
194       If you need to add paths to the end of INCLUDE_PATH, there is also an
195       include_path() accessor available:
196
197           push( @{ $c->view('TT')->include_path }, qw/path/ );
198
199       Note that if you use include_path() to add extra paths to INCLUDE_PATH,
200       you MUST check for duplicate paths. Without such checking, the above
201       code will add "path" to INCLUDE_PATH at every request, causing a memory
202       leak.
203
204       A safer approach is to use include_path() to overwrite the array of
205       paths rather than adding to it. This eliminates both the need to
206       perform duplicate checking and the chance of a memory leak:
207
208           @{ $c->view('TT')->include_path } = qw/path another_path/;
209
210       If you are calling "render" directly then you can specify dynamic paths
211       by having a "additional_template_paths" key with a value of additonal
212       directories to search. See "CAPTURING TEMPLATE OUTPUT" for an example
213       showing this.
214
215   RENDERING VIEWS
216       The view plugin renders the template specified in the "template" item
217       in the stash.
218
219           sub message : Global {
220               my ( $self, $c ) = @_;
221               $c->stash->{template} = 'message.tt2';
222               $c->forward( $c->view('TT') );
223           }
224
225       If a stash item isn't defined, then it instead uses the stringification
226       of the action dispatched to (as defined by $c->action) in the above
227       example, this would be "message", but because the default is to append
228       '.tt', it would load "root/message.tt".
229
230       The items defined in the stash are passed to the Template Toolkit for
231       use as template variables.
232
233           sub default : Private {
234               my ( $self, $c ) = @_;
235               $c->stash->{template} = 'message.tt2';
236               $c->stash->{message}  = 'Hello World!';
237               $c->forward( $c->view('TT') );
238           }
239
240       A number of other template variables are also added:
241
242           c      A reference to the context object, $c
243           base   The URL base, from $c->req->base()
244           name   The application name, from $c->config->{ name }
245
246       These can be accessed from the template in the usual way:
247
248       <message.tt2>:
249
250           The message is: [% message %]
251           The base is [% base %]
252           The name is [% name %]
253
254       The output generated by the template is stored in "$c->response->body".
255
256   CAPTURING TEMPLATE OUTPUT
257       If you wish to use the output of a template for some other purpose than
258       displaying in the response, e.g. for sending an email, this is possible
259       using Catalyst::Plugin::Email and the render method:
260
261         sub send_email : Local {
262           my ($self, $c) = @_;
263
264           $c->email(
265             header => [
266               To      => 'me@localhost',
267               Subject => 'A TT Email',
268             ],
269             body => $c->view('TT')->render($c, 'email.tt', {
270               additional_template_paths => [ $c->config->{root} . '/email_templates'],
271               email_tmpl_param1 => 'foo'
272               }
273             ),
274           );
275         # Redirect or display a message
276         }
277
278   TEMPLATE PROFILING
279       See "TIMER" property of the config method.
280
281   METHODS
282   new
283       The constructor for the TT view. Sets up the template provider, and
284       reads the application config.
285
286   process($c)
287       Renders the template specified in "$c->stash->{template}" or
288       "$c->action" (the private name of the matched action).  Calls render to
289       perform actual rendering. Output is stored in "$c->response->body".
290
291       It is possible to forward to the process method of a TT view from
292       inside Catalyst like this:
293
294           $c->forward('View::TT');
295
296       N.B. This is usually done automatically by
297       Catalyst::Action::RenderView.
298
299   render($c, $template, \%args)
300       Renders the given template and returns output. Throws a
301       Template::Exception object upon error.
302
303       The template variables are set to %$args if $args is a hashref, or
304       $"$c->stash" otherwise. In either case the variables are augmented with
305       "base" set to " << $c-"req->base >>, "c" to $c and "name" to
306       "$c->config->{name}". Alternately, the "CATALYST_VAR" configuration
307       item can be defined to specify the name of a template variable through
308       which the context reference ($c) can be accessed. In this case, the
309       "c", "base" and "name" variables are omitted.
310
311       $template can be anything that Template::process understands how to
312       process, including the name of a template file or a reference to a test
313       string.  See Template::process for a full list of supported formats.
314
315       To use the render method outside of your Catalyst app, just pass a
316       undef context.  This can be useful for tests, for instance.
317
318       It is possible to forward to the render method of a TT view from inside
319       Catalyst to render page fragments like this:
320
321           my $fragment = $c->forward("View::TT", "render", $template_name, $c->stash->{fragment_data});
322
323       Backwards compatibility note
324
325       The render method used to just return the Template::Exception object,
326       rather than just throwing it. This is now deprecated and instead the
327       render method will throw an exception for new applications.
328
329       This behaviour can be activated (and is activated in the default
330       skeleton configuration) by using "render_die => 1". If you rely on the
331       legacy behaviour then a warning will be issued.
332
333       To silence this warning, set "render_die => 0", but it is recommended
334       you adjust your code so that it works with "render_die => 1".
335
336       In a future release, "render_die => 1" will become the default if
337       unspecified.
338
339   template_vars
340       Returns a list of keys/values to be used as the catalyst variables in
341       the template.
342
343   config
344       This method allows your view subclass to pass additional settings to
345       the TT configuration hash, or to set the options as below:
346
347   paths
348       The list of paths TT will look for templates in.
349
350   "CATALYST_VAR"
351       Allows you to change the name of the Catalyst context object. If set,
352       it will also remove the base and name aliases, so you will have access
353       them through <context>.
354
355       For example:
356
357           MyApp->config({
358               name     => 'MyApp',
359               root     => MyApp->path_to('root'),
360               'View::TT' => {
361                   CATALYST_VAR => 'Catalyst',
362               },
363           });
364
365       message.tt2:
366
367           The base is [% Catalyst.req.base %]
368           The name is [% Catalyst.config.name %]
369
370   "TIMER"
371       If you have configured Catalyst for debug output, and turned on the
372       TIMER setting, "Catalyst::View::TT" will enable profiling of template
373       processing (using Template::Timer). This will embed HTML comments in
374       the output from your templates, such as:
375
376           <!-- TIMER START: process mainmenu/mainmenu.ttml -->
377           <!-- TIMER START: include mainmenu/cssindex.tt -->
378           <!-- TIMER START: process mainmenu/cssindex.tt -->
379           <!-- TIMER END: process mainmenu/cssindex.tt (0.017279 seconds) -->
380           <!-- TIMER END: include mainmenu/cssindex.tt (0.017401 seconds) -->
381
382           ....
383
384           <!-- TIMER END: process mainmenu/footer.tt (0.003016 seconds) -->
385
386   "TEMPLATE_EXTENSION"
387       a sufix to add when looking for templates bases on the "match" method
388       in Catalyst::Request.
389
390       For example:
391
392         package MyApp::Controller::Test;
393         sub test : Local { .. }
394
395       Would by default look for a template in <root>/test/test. If you set
396       TEMPLATE_EXTENSION to '.tt', it will look for <root>/test/test.tt.
397
398   "PROVIDERS"
399       Allows you to specify the template providers that TT will use.
400
401           MyApp->config({
402               name     => 'MyApp',
403               root     => MyApp->path_to('root'),
404               'View::TT' => {
405                   PROVIDERS => [
406                       {
407                           name    => 'DBI',
408                           args    => {
409                               DBI_DSN => 'dbi:DB2:books',
410                               DBI_USER=> 'foo'
411                           }
412                       }, {
413                           name    => '_file_',
414                           args    => {}
415                       }
416                   ]
417               },
418           });
419
420       The 'name' key should correspond to the class name of the provider you
421       want to use.  The _file_ name is a special case that represents the
422       default TT file-based provider.  By default the name is will be
423       prefixed with 'Template::Provider::'.  You can fully qualify the name
424       by using a unary plus:
425
426           name => '+MyApp::Provider::Foo'
427
428       You can also specify the 'copy_config' key as an arrayref, to copy
429       those keys from the general config, into the config for the provider:
430
431           DEFAULT_ENCODING    => 'utf-8',
432           PROVIDERS => [
433               {
434                   name    => 'Encoding',
435                   copy_config => [qw(DEFAULT_ENCODING INCLUDE_PATH)]
436               }
437           ]
438
439       This can prove useful when you want to use the
440       additional_template_paths hack in your own provider, or if you need to
441       use Template::Provider::Encoding
442
443   HELPERS
444       The Catalyst::Helper::View::TT and Catalyst::Helper::View::TTSite
445       helper modules are provided to create your view module.  There are
446       invoked by the myapp_create.pl script:
447
448           $ script/myapp_create.pl view TT TT
449
450           $ script/myapp_create.pl view TT TTSite
451
452       The Catalyst::Helper::View::TT module creates a basic TT view module.
453       The Catalyst::Helper::View::TTSite module goes a little further.  It
454       also creates a default set of templates to get you started.  It also
455       configures the view module to locate the templates automatically.
456

NOTES

458       If you are using the CGI module inside your templates, you will
459       experience that the Catalyst server appears to hang while rendering the
460       web page. This is due to the debug mode of CGI (which is waiting for
461       input in the terminal window). Turning off the debug mode using the
462       "-no_debug" option solves the problem, eg.:
463
464           [% USE CGI('-no_debug') %]
465

SEE ALSO

467       Catalyst, Catalyst::Helper::View::TT, Catalyst::Helper::View::TTSite,
468       Template::Manual
469

AUTHORS

471       Sebastian Riedel, "sri@cpan.org"
472
473       Marcus Ramberg, "mramberg@cpan.org"
474
475       Jesse Sheidlower, "jester@panix.com"
476
477       Andy Wardley, "abw@cpan.org"
478
480       This program is free software. You can redistribute it and/or modify it
481       under the same terms as Perl itself.
482
483
484
485perl v5.12.1                      2010-04-07             Catalyst::View::TT(3)
Impressum