2 Catalyst::View::TT - Template View Class
5 # use the helper to create View myapp_create.pl view TT TT
7 # configure in lib/MyApp.pm
9 our $ROOT = '/home/dent/catalyst/MyApp';
15 # any TT configurations items go here
17 "$ROOT/templates/src",
20 PRE_PROCESS => 'config/main',
21 WRAPPER => 'site/wrapper',
23 # two optional config items
24 CATALYST_VAR => 'Catalyst',
29 # render view from lib/MyApp.pm or lib/MyApp::C::SomeController.pm
31 sub message : Global {
32 my ( $self, $c ) = @_;
33 $c->stash->{template} = 'message.tt2';
34 $c->stash->{message} = 'Hello World!';
35 $c->forward('MyApp::V::TT');
38 # access variables from template
40 The message is: [% message %].
42 # example when CATALYST_VAR is set to 'Catalyst'
43 Context is [% Catalyst %]
44 The base is [% Catalyst.req.base %]
45 The name is [% Catalyst.config.name %]
47 # example when CATALYST_VAR isn't set
49 The base is [% base %]
50 The name is [% name %]
53 This is the Catalyst view class for the Template Toolkit. Your
54 application should defined a view class which is a subclass of this
55 module. The easiest way to achieve this is using the myapp_create.pl
56 script (where myapp should be replaced with whatever your application is
57 called). This script is created as part of the Catalyst setup.
59 $ script/myapp_create.pl view TT TT
61 This creates a MyApp::V::TT.pm module in the lib directory (again,
62 replacing "MyApp" with the name of your application) which looks
65 package FooBar::V::TT;
68 use base 'Catalyst::View::TT';
70 __PACKAGE__->config->{DEBUG} = 'all';
72 Now you can modify your action handlers in the main application and/or
73 controllers to forward to your view class. You might choose to do this
74 in the end() method, for example, to automatically forward all actions
77 # In MyApp or MyApp::Controller::SomeController
81 $c->forward('MyApp::V::TT');
85 There are a three different ways to configure your view class. The first
86 way is to call the "config()" method in the view subclass. This happens
87 when the module is first loaded.
92 use base 'Catalyst::View::TT';
94 our $ROOT = '/home/dent/catalyst/MyApp';
96 MyApp::V::TT->config({
97 INCLUDE_PATH => ["$ROOT/templates/src", "$ROOT/templates/lib"],
98 PRE_PROCESS => 'config/main',
99 WRAPPER => 'site/wrapper',
102 The second way is to define a "new()" method in your view subclass. This
103 performs the configuration when the view object is created, shortly
104 after being loaded. Remember to delegate to the base class "new()"
105 method (via "$self->NEXT::new()" in the example below) after performing
111 INCLUDE_PATH => ["$ROOT/templates/src", "$ROOT/templates/lib"],
112 PRE_PROCESS => 'config/main',
113 WRAPPER => 'site/wrapper',
115 return $self->NEXT::new(@_);
118 The final, and perhaps most direct way, is to define a class item in
119 your main application configuration, again by calling the uniquitous
120 "config()" method. The items in the class hash are added to those
121 already defined by the above two methods. This happens in the base class
122 new() method (which is one reason why you must remember to call it via
123 "NEXT" if you redefine the "new()" method in a subclass).
130 our $ROOT = '/home/dent/catalyst/MyApp';
136 INCLUDE_PATH => ["$ROOT/templates/src", "$ROOT/templates/lib"],
137 PRE_PROCESS => 'config/main',
138 WRAPPER => 'site/wrapper',
142 Note that any configuration items defined by one of the earlier methods
143 will be overwritten by items of the same name provided by the latter
147 The view plugin renders the template specified in the "template" item in
150 sub message : Global {
151 my ( $self, $c ) = @_;
152 $c->stash->{template} = 'message.tt2';
153 $c->forward('MyApp::V::TT');
156 If a class item isn't defined, then it instead uses the current match,
157 as returned by "$c->match". In the above example, this would be
160 The items defined in the stash are passed to the Template Toolkit for
161 use as template variables.
163 sub message : Global { sub default : Private { my ( $self, $c ) = @_;
164 $c->stash->{template} = 'message.tt2'; $c->stash->{message} = 'Hello
165 World!'; $c->forward('MyApp::V::TT'); }
167 A number of other template variables are also added:
169 c A reference to the context object, $c
170 base The URL base, from $c->req->base()
171 name The application name, from $c->config->{ name }
173 These can be accessed from the template in the usual way:
177 The message is: [% message %]
178 The base is [% base %]
179 The name is [% name %]
181 If you prefer, you can set the "CATALYST_VAR" configuration item to
182 define the name of a template variable through which the context can be
189 CATALYST_VAR => 'Catalyst',
195 The base is [% Catalyst.req.base %]
196 The name is [% Catalyst.config.name %]
198 The output generated by the template is stored in
199 "$c->response->output".
202 If you have configured Catalyst for debug output, "Catalyst::View::TT"
203 will enable profiling of template processing (using Template::Timer).
204 This will embed HTML comments in the output from your templates, such
207 <!-- TIMER START: process mainmenu/mainmenu.ttml -->
208 <!-- TIMER START: include mainmenu/cssindex.tt -->
209 <!-- TIMER START: process mainmenu/cssindex.tt -->
210 <!-- TIMER END: process mainmenu/cssindex.tt (0.017279 seconds) -->
211 <!-- TIMER END: include mainmenu/cssindex.tt (0.017401 seconds) -->
215 <!-- TIMER END: process mainmenu/footer.tt (0.003016 seconds) -->
217 You can suppress template profiling by setting the "TIMER" configuration
218 item to a false value.
227 new The constructor for the TT view. Sets up the template provider, and
228 reads the application config.
231 Renders the template specified in "$c->stash->{template}" or
232 "$c->request->match". Template variables are set up from the
233 contents of "$c->stash", augmented with "base" set to
234 "$c->req->base", "c" to $c and "name" to "$c->config->{name}".
235 Alternately, the "CATALYST_VAR" configuration item can be defined to
236 specify the name of a template variable through which the context
237 reference ($c) can be accessed. In this case, the "c", "base" and
238 "name" variables are omitted. Output is stored in
239 "$c->response->output".
242 This method allows your view subclass to pass additional settings to
243 the TT configuration hash, or to set the "CATALYST_VAR" and "TIMER"
247 The Catalyst::Helper::View::TT and Catalyst::Helper::View::TTSite helper
248 modules are provided to create your view module. There are invoked by
249 the myapp_create.pl script:
251 $ script/myapp_create.pl view TT TT
253 $ script/myapp_create.pl view TT TTSite
255 The Catalyst::Helper::View::TT module creates a basic TT view module.
256 The Catalyst::Helper::View::TTSite module goes a little further. It also
257 creates a default set of templates to get you started. It also
258 configures the view module to locate the templates automatically.
261 Catalyst, Catalyst::Helper::View::TT, Catalyst::Helper::View::TTSite,
265 Sebastian Riedel, "sri@cpan.org"
267 Marcus Ramberg, "mramberg@cpan.org"
269 Jesse Sheidlower, "jester@panix.com"
271 Andy Wardley, "abw@cpan.org"
274 This program is free software, you can redistribute it and/or modify it
275 under the same terms as Perl itself.