1 package Maypole::View::TT;
2 use base 'Maypole::View::Base';
3 use Maypole::Constants;
5 use File::Spec::Functions qw(catdir tmpdir);
8 { local $/; $error_template = <DATA>; }
14 my ( $self, $r ) = @_;
15 unless ($self->{tt}) {
16 my $view_options = $r->config->view_options || {};
17 $self->{provider} = Template::Provider->new($view_options);
18 $self->{tt} = Template->new({
20 LOAD_TEMPLATES => [ $self->{provider} ],
24 $self->{provider}->include_path([ $self->paths($r) ]);
26 my $template_file = $r->template;
28 my $ext = $r->config->template_extension;
29 $template_file .= $ext if defined $ext;
32 my $processed_ok = eval{$self->{tt}->process($template_file, { $self->vars($r) }, \$output );};
34 $r->{output} = $output;
38 warn "fatal error in template '$template_file' : $@\n";
39 $r->{error} = "fatal error in template '$template_file' : $@";
41 warn "TT error for template '$template_file'\n" . $self->{tt}->error;
42 $r->{error} = "TT error for template '$template_file'\n" . $self->{tt}->error;
50 my ($self, $r, $error, $type) = @_;
52 # Need to be very careful here.
53 my $tt = Template->new;
54 unless (ref $r->{config}) {
55 warn "no config for this request\n";
56 $error .= '<br> There was a problem finding configuration for this request';
59 if ($tt->process(\$error_template,
60 { err_type => $type, error => $error,
61 config => { (%{$r->{config}}) },
62 request => $r, # We have that at least
63 eval{$self->vars($r)} }, \$output )) {
64 $r->{output} = $output;
65 if ($tt->error) { $r->{output} = "<html><body>Even the error template
66 errored - ".$tt->error."</body></html>"; }
67 $r->{content_type} ||= "text/html";
68 $r->{document_encoding} ||= "utf-8";
77 Maypole::View::TT - A Template Toolkit view class for Maypole
81 BeerDB->config->view("Maypole::View::TT"); # The default anyway
83 # Set some Template Toolkit options
84 BeerDB->config->view_options( {
86 COMPILE_DIR => '/var/tmp/mysite/templates',
101 This is the default view class for Maypole; it uses the Template Toolkit to fill
102 in templates with the objects produced by Maypole's model classes. Please see
103 the L<Maypole manual|Maypole::Manual>, and in particular, the
104 L<view|Maypole::Manual::View> chapter for the template variables available and
105 for a refresher on how template components are resolved.
107 The underlying Template toolkit object is configured through
108 C<$r-E<gt>config-E<gt>view_options>. See L<Template|Template> for available
115 Processes the template and sets the output. See L<Maypole::View::Base>
119 Reports the details of an error, current state and parameters
123 =head1 TEMPLATE TOOLKIT INTRODUCTION
125 The Template Toolkit uses it's own mini language described in
126 L<Template::Manual::Directives>.
128 A simple example would be :
134 Dear [% title %] [% surname %],
135 Thank you for your letter dated [% your.date %]. This is to
136 confirm that we have received it and will respond with a more
137 detailed response as soon as possible. In the mean time, we
138 enclose more details of ...
142 TT uses '[%' and '%]' (by default) to delimit directives within a template, and
143 the simple directives above just display the value of variable named within
144 those delimiters -- [% title %] will be replaced inline with the value of the
145 'title' variable passed in the 'stash' to the template when it is processed.
147 You can access nested data through the dot ('.') operator, which will
148 dereference array or hash elements, but can also be used to call methods on
149 objects, i.e. '[% name.salutation("Dear %s,") %]'. The other main operator is
150 underscore ('_'), which will concatonate strings or variables.
152 The value returned by a directive replaces the directive inline when the
153 template is processes, you can also SET a value which will not return anything,
154 or CALL a method or operation which will also not return anything.
156 You can specify expressions using the logical (and, or, not, ?:) and mathematic
157 operators (+ - * / % mod div).
159 Results of TT commands are interpolated in the place of the template tags, unless
160 using SET or CALL, i.e. [% SET foo = 1 %], [% GET foo.bar('quz'); %]
164 [% template.title or default.title %]
168 [% order.nitems ? checkout(order.total) : 'no items' %]
172 TT allows you to include or re-use templates through it's INCLUDE, PROCESS and
173 INSERT directives, which are fairly self explainatory. You can also re-use parts
174 of template with the BLOCK or MACRO directives.
176 Conditional and Looping constructs are simple and powerful, and TT provides an
177 inbuilt iterator and helper functions and classes that make life sweet.
179 Conditional directives are IF, UNLESS, ELSIF, ELSE and behave as they would in
185 Hello [% name %], does your mother know you're using her AOL account?
187 Sorry, you're not old enough to enter (and too dumb to lie about your age)
192 [% UNLESS text_mode %] [% INCLUDE biglogo %] [% END %]
196 Looping directives are FOREACH, LAST and BREAK.
198 FOREACH loops through a HASH or ARRAY processing the enclosed block for each
201 Looping through an array
203 [% FOREACH i = items %]
207 Looping through a hash
209 [% FOREACH u IN users %]
210 * [% u.key %] : [% u.value %]
213 Looping through an array of hashes
215 [% FOREACH user IN userlist %]
216 * [% user.id %] [% user.name %]
219 The LAST and BREAK directive can be used to exit the loop.
221 The FOREACH directive is implemented using the Template::Iterator module. A
222 reference to the iterator object for a FOREACH directive is implicitly available
223 in the 'loop' variable. The loop iterator object provides a selection of methods
224 including size(), max(), first(), last(), count(), etc
228 [% FOREACH item IN [ 'foo', 'bar', 'baz' ] -%]
229 [%- "<ul>\n" IF loop.first %]
230 <li>[% loop.count %]/[% loop.size %]: [% item %]
231 [%- "</ul>\n" IF loop.last %]
236 See Template::Iterator for further details on looping and the Iterator.
238 You might notice the minus ('-') operator in the example above, it is used to
239 remove a newline before or after a directive so that you can layout the Template
240 logic as above but the resulting output will look exactly how you require it.
242 You will also frequently see comments and multi-line directives, # at the start
243 of a directive marks it as a comment, i.e. '[%# this is a comment %]'. A
244 multiline directive looks like :
250 You can see that lines are terminated with a semi-colon (';') unless the
251 delimter ('%]') closes the directive.
253 For full details of the Template Toolkit see Template::Manual and
254 Template::Manual::Directives, you can also check the website, mailing list or
255 the Template Toolkit book published by O Reilly.
257 =head1 TEMPLATE PLUGINS, FILTERS AND MACROS
259 The Template Toolkit has a popular and powerful selection of Plugins and
262 TT Plugins provide additional functionality within Templates, from accessing CGI
263 and databases directly, handling paging or simple integration with Class::DBI
264 (for those rare occasions where you don't actually need Maypole). See
265 L<Template::Manual::Plugins>.
267 One plugin that is indispensible when using Maypole and the Template View is
268 C<Template::Plugin::Class> -- This allows you to import and use any class
269 installed within a template. For example :
273 [% USE foo = Class('Foo') %]
278 Would do the equivilent of 'use Foo; Foo->bar;' in perl. See
279 L<Template::Plugin::Class> for details.
281 TT Filters process strings or blocks within a template, allowing you to
282 truncate, format, escape or encode trivially. A useful selection is included
283 with Template Toolkit and they can also be found on CPAN or can be written
284 easily. See L<Template::Manual::Filters>.
286 TT Macros allow you to reuse small blocks of content, directives, etc. The MACRO
287 directive allows you to define a directive or directive block which is then
288 evaluated each time the macro is called. Macros can be passed named parameters
291 Once a MACRO is defined within a template or 'include'd template it can be used
292 as if it were a native TT directive. Maypole provides a selection of powerful
293 and useful macros in the templates/ directory of the package and these are used
294 in the beerdb and default templates. See the MACRO section of the
295 L<Template::Manual::Directives> documentation.
297 =head1 ACCESSING MAYPOLE VALUES
301 You can access the request in your templates in order to see the action, table, etc as well
302 as parameters passed through forms :
306 Hello [% request.params.forename %] [% request.params.surname %] !
310 Are you want to [% request.action %] in the [% request.table %] ?
314 You can access your maypole application configuration through the config variable :
316 <link base="[% config.uri_base %]"/>
318 =head2 object and objects
320 Objects are passed to the request using r->objects($arrayref) and are accessed in the templates
321 as an array called objects.
323 [% FOR objects %] <a href="[% config.uri_base %]/[% request.table %]/view/[% object.id %]"> [% object %] </a> [% END %]
325 =head1 MAYPOLE MACROS AND FILTERS
327 Maypole provides a collection of useful and powerful macros in the templates/factory/macros
328 and other templates. These can be used in any template with [% PROCESS templatename %].
332 This creates an <A HREF="..."> to a command in the Apache::MVC system by
333 catenating the base URL, table, command, and any arguments.
335 =head2 maybe_link_view
337 C<maybe_link_view> takes something returned from the database - either
338 some ordinary data, or an object in a related class expanded by a
339 has-a relationship. If it is an object, it constructs a link to the view
340 command for that object. Otherwise, it just displays the data.
344 This is an include template rather than a macro, and it controls the pager
345 display at the bottom (by default) of the factory list and search views/template.
346 It expects a C<pager> template argument which responds to the L<Data::Page> interface.
348 This macro is in the pager template and used as :
352 Maypole provides a pager for list and search actions, otherwise you can
353 provide a pager in the template using Template::Plugin::Pagination.
355 [% USE pager = Pagination(objects, page.current, page.rows) %]
359 The pager will use a the request action as the action in the url unless the
360 pager_action variable is set, which it will use instead if available.
373 <html><head><title>Maypole error page</title>
374 <style type="text/css">
375 body { background-color:#7d95b5; font-family: sans-serif}
376 p { background-color: #fff; padding: 5px; }
377 pre { background-color: #fff; padding: 5px; border: 1px dotted black }
380 .lhs {background-color: #ffd; }
381 .rhs {background-color: #dff; }
384 <h1> Maypole application error </h1>
386 <p> This application living at <code>[%request.config.uri_base%]</code>,
387 [%request.config.application_name || "which is unnamed" %], has
388 produced an error. The adminstrator should be able to understand
389 this error message and fix the problem.</p>
391 <h2> Some basic facts </h2>
393 <p> The error was found in the [% err_type %] stage of processing
394 the path "[% request.path %]". The error text returned was:
400 <h2> Request details </h2>
403 [% FOR thing = ["model_class", "table", "template", "path",
404 "content_type", "document_encoding", "action", "args", "objects"] %]
405 <tr> <td class="lhs"> [%thing %] </td> <td class="rhs"> [%
406 request.$thing.list.join(" , ") %] </td></tr>
410 <h2> Application configuration </h2>
412 [% FOR thing = config.keys %]
413 <tr> <td class="lhs"> [%thing %] </td> <td class="rhs"> [%
414 config.$thing.list.join(" , ") %] </td></tr>