1 package Maypole::View::TT;
2 use base 'Maypole::View::Base';
3 use Maypole::Constants;
5 use File::Spec::Functions qw(catdir tmpdir);
11 my ( $self, $r ) = @_;
13 unless ($self->{tt}) {
14 my $view_options = $r->config->view_options || {};
15 $self->{provider} = Template::Provider->new($view_options);
16 $self->{tt} = Template->new({
18 LOAD_TEMPLATES => [ $self->{provider} ],
22 $self->{provider}->include_path([ $self->paths($r) ]);
24 my $template_file = $r->template;
25 my $ext = $r->config->template_extension;
26 $template_file .= $ext if defined $ext;
29 if ($self->{tt}->process($template_file, { $self->vars($r) }, \$output )) {
30 $r->{output} = $output;
34 $r->{error} = "TT error for template '$template_file'\n" . $self->{tt}->error;
43 Maypole::View::TT - A Template Toolkit view class for Maypole
47 BeerDB->config->view("Maypole::View::TT"); # The default anyway
49 # Set some Template Toolkit options
50 BeerDB->config->view_options( {
52 COMPILE_DIR => '/var/tmp/mysite/templates',
67 This is the default view class for Maypole; it uses the Template Toolkit to fill
68 in templates with the objects produced by Maypole's model classes. Please see
69 the L<Maypole manual|Maypole::Manual>, and in particular, the
70 L<view|Maypole::Manual::View> chapter for the template variables available and
71 for a refresher on how template components are resolved.
73 The underlying Template toolkit object is configured through
74 C<$r-E<gt>config-E<gt>view_options>. See L<Template|Template> for available
81 Processes the template and sets the output. See L<Maypole::View::Base>
85 =head1 TEMPLATE TOOLKIT INTRODUCTION
87 The Template Toolkit uses it's own mini language described in
88 L<Template::Manual::Directives>.
90 A simple example would be :
96 Dear [% title %] [% surname %],
97 Thank you for your letter dated [% your.date %]. This is to
98 confirm that we have received it and will respond with a more
99 detailed response as soon as possible. In the mean time, we
100 enclose more details of ...
104 TT uses '[%' and '%]' (by default) to delimit directives within a template, and
105 the simple directives above just display the value of variable named within
106 those delimiters -- [% title %] will be replaced inline with the value of the
107 'title' variable passed in the 'stash' to the template when it is processed.
109 You can access nested data through the dot ('.') operator, which will
110 dereference array or hash elements, but can also be used to call methods on
111 objects, i.e. '[% name.salutation("Dear %s,") %]'. The other main operator is
112 underscore ('_'), which will concatonate strings or variables.
114 The value returned by a directive replaces the directive inline when the
115 template is processes, you can also SET a value which will not return anything,
116 or CALL a method or operation which will also not return anything.
118 You can specify expressions using the logical (and, or, not, ?:) and mathematic
119 operators (+ - * / % mod div).
123 [% template.title or default.title %]
127 [% order.nitems ? checkout(order.total) : 'no items' %]
131 TT allows you to include or re-use templates through it's INCLUDE, PROCESS and
132 INSERT directives, which are fairly self explainatory. You can also re-use parts
133 of template with the BLOCK or MACRO directives.
135 Conditional and Looping constructs are simple and powerful, and TT provides an
136 inbuilt iterator and helper functions and classes that make life sweet.
138 Conditional directives are IF, UNLESS, ELSIF, ELSE and behave as they would in
144 Hello [% name %], does your mother know you're using her AOL account?
146 Sorry, you're not old enough to enter (and too dumb to lie about your age)
151 [% UNLESS text_mode %] [% INCLUDE biglogo %] [% END %]
155 Looping directives are FOREACH, LAST and BREAK.
157 FOREACH loops through a HASH or ARRAY processing the enclosed block for each
160 Looping through an array
162 [% FOREACH i = items %]
166 Looping through a hash
168 [% FOREACH u IN users %]
169 * [% u.key %] : [% u.value %]
172 Looping through an array of hashes
174 [% FOREACH user IN userlist %]
175 * [% user.id %] [% user.name %]
178 The LAST and BREAK directive can be used to exit the loop.
180 The FOREACH directive is implemented using the Template::Iterator module. A
181 reference to the iterator object for a FOREACH directive is implicitly available
182 in the 'loop' variable. The loop iterator object provides a selection of methods
183 including size(), max(), first(), last(), count(), etc
187 [% FOREACH item IN [ 'foo', 'bar', 'baz' ] -%]
188 [%- "<ul>\n" IF loop.first %]
189 <li>[% loop.count %]/[% loop.size %]: [% item %]
190 [%- "</ul>\n" IF loop.last %]
195 See Template::Iterator for further details on looping and the Iterator.
197 You might notice the minus ('-') operator in the example above, it is used to
198 remove a newline before or after a directive so that you can layout the Template
199 logic as above but the resulting output will look exactly how you require it.
201 You will also frequently see comments and multi-line directives, # at the start
202 of a directive marks it as a comment, i.e. '[%# this is a comment %]'. A
203 multiline directive looks like :
209 You can see that lines are terminated with a semi-colon (';') unless the
210 delimter ('%]') closes the directive.
212 For full details of the Template Toolkit see Template::Manual and
213 Template::Manual::Directives, you can also check the website, mailing list or
214 the Template Toolkit book published by O Reilly.
216 =head1 TEMPLATE PLUGINS, FILTERS AND MACROS
218 The Template Toolkit has a popular and powerful selection of Plugins and
221 TT Plugins provide additional functionality within Templates, from accessing CGI
222 and databases directly, handling paging or simple integration with Class::DBI
223 (for those rare occasions where you don't actually need Maypole). See
224 L<Template::Manual::Plugins>.
226 One plugin that is indispensible when using Maypole and the Template View is
227 C<Template::Plugin::Class> -- This allows you to import and use any class
228 installed within a template. For example :
232 [% USE foo = Class('Foo') %]
237 Would do the equivilent of 'use Foo; Foo->bar;' in perl. See
238 L<Template::Plugin::Class> for details.
240 TT Filters process strings or blocks within a template, allowing you to
241 truncate, format, escape or encode trivially. A useful selection is included
242 with Template Toolkit and they can also be found on CPAN or can be written
243 easily. See L<Template::Manual::Filters>.
245 TT Macros allow you to reuse small blocks of content, directives, etc. The MACRO
246 directive allows you to define a directive or directive block which is then
247 evaluated each time the macro is called. Macros can be passed named parameters
250 Once a MACRO is defined within a template or 'include'd template it can be used
251 as if it were a native TT directive. Maypole provides a selection of powerful
252 and useful macros in the templates/ directory of the package and these are used
253 in the beerdb and default templates. See the MACRO section of the
254 L<Template::Manual::Directives> documentation.
256 =head1 MAYPOLE MACROS AND FILTERS
258 Maypole provides a collection of useful and powerful macros in the templates/factory/macros
259 and other templates. These can be used in any template with [% PROCESS templatename %].
263 This creates an <A HREF="..."> to a command in the Apache::MVC system by
264 catenating the base URL, table, command, and any arguments.
266 =head2 maybe_link_view
268 C<maybe_link_view> takes something returned from the database - either
269 some ordinary data, or an object in a related class expanded by a
270 has-a relationship. If it is an object, it constructs a link to the view
271 command for that object. Otherwise, it just displays the data.
275 This is an include template rather than a macro, and it controls the pager
276 display at the bottom (by default) of the factory list and search views/template.
277 It expects a C<pager> template argument which responds to the L<Data::Page> interface.
279 This macro is in the pager template and used as :
283 Maypole provides a pager for list and search actions, otherwise you can
284 provide a pager in the template using Template::Plugin::Pagination.
286 [% USE pager = Pagination(objects, page.current, page.rows) %]
290 The pager will use a the request action as the action in the url unless the
291 pager_action variable is set, which it will use instead if available.