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',
57 [%# Template Toolkit directives and maypole macros go here %]
61 This is the default view class for Maypole; it uses the Template Toolkit to
62 fill in templates with the objects produced by Maypole's model classes. Please
63 see the L<Maypole manual|Maypole::Manual>, and in particular, the
64 L<view|Maypole::Manual::View> chapter for the template variables available and
65 for a refresher on how template components are resolved.
67 The underlying Template toolkit object is configured through
68 C<$r-E<gt>config-E<gt>view_options>. See L<Template|Template> for available
75 Processes the template and sets the output. See L<Maypole::View::Base>
79 =head1 TEMPLATE TOOLKIT INTRODUCTION
81 The Template Toolkit uses it's own mini language described in L<Template::Manual::Directives>.
83 A simple example would be :
89 Dear [% title %] [% surname %],
90 Thank you for your letter dated [% your.date %]. This is to
91 confirm that we have received it and will respond with a more
92 detailed response as soon as possible. In the mean time, we
93 enclose more details of ...
97 TT uses '[%' and '%]' (by default) to delimit directives within a template, and the simple directives
98 above just display the value of variable named within those delimiters -- [% title %] will be replaced
99 inline with the value of the 'title' variable passed in the 'stash' to the template when it is processed.
101 You can access nested data through the dot ('.') operator, which will dereference array or hash elements,
102 but can also be used to call methods on objects, i.e. '[% name.salutation("Dear %s,") %]'. The other main
103 operator is underscore ('_'), which will concatonate strings or variables.
105 The value returned by a directive replaces the directive inline when the template is processes, you can also
106 SET a value which will not return anything, or CALL a method or operation which will also not return anything.
108 You can specify expressions using the logical (and, or, not, ?:) and mathematic operators (+ - * / % mod div).
112 [% template.title or default.title %]
116 [% order.nitems ? checkout(order.total) : 'no items' %]
120 TT allows you to include or re-use templates through it's INCLUDE, PROCESS and INSERT directives, which
121 are fairly self explainatory. You can also re-use parts of template with the BLOCK or MACRO directives.
123 Conditional and Looping constructs are simple and powerful, and TT provides an inbuilt iterator and helper
124 functions and classes that make life sweet.
126 Conditional directives are IF, UNLESS, ELSIF, ELSE and behave as they would in perl :
131 Hello [% name %], does your mother know you're using her AOL account?
133 Sorry, you're not old enough to enter (and too dumb to lie about your age)
138 [% UNLESS text_mode %] [% INCLUDE biglogo %] [% END %]
142 Looping directives are FOREACH, LAST and BREAK.
144 FOREACH loops through a HASH or ARRAY processing the enclosed block for each element.
146 Looping through an array
148 [% FOREACH i = items %]
152 Looping through a hash
154 [% FOREACH u IN users %]
155 * [% u.key %] : [% u.value %]
158 Looping through an array of hashes
160 [% FOREACH user IN userlist %]
161 * [% user.id %] [% user.name %]
164 The LAST and BREAK directive can be used to exit the loop.
166 The FOREACH directive is implemented using the Template::Iterator module. A reference
167 to the iterator object for a FOREACH directive is implicitly available in the 'loop' variable.
168 The loop iterator object provides a selection of methods including size(), max(), first(), last(),
173 [% FOREACH item IN [ 'foo', 'bar', 'baz' ] -%]
174 [%- "<ul>\n" IF loop.first %]
175 <li>[% loop.count %]/[% loop.size %]: [% item %]
176 [%- "</ul>\n" IF loop.last %]
181 See Template::Iterator for further details on looping and the Iterator.
183 You might notice the minus ('-') operator in the example above, it is used to remove a newline
184 before or after a directive so that you can layout the Template logic as above but the resulting
185 output will look exactly how you require it.
187 You will also frequently see comments and multi-line directives, # at the start of a directive
188 marks it as a comment, i.e. '[%# this is a comment %]'. A multiline directive looks like :
194 You can see that lines are terminated with a semi-colon (';') unless the delimter ('%]') closes the directive.
196 For full details of the Template Toolkit see Template::Manual and Template::Manual::Directives, you can
197 also check the website, mailing list or the Template Toolkit book published by O Reilly.
199 =head1 TEMPLATE PLUGINS, FILTERS AND MACROS
201 The Template Toolkit has a popular and powerful selection of Plugins and Filters.
203 TT Plugins provide additional functionality within Templates, from accessing CGI and
204 databases directly, handling paging or simple integration with Class::DBI (for those
205 rare occasions where you don't actually need Maypole). See L<Template::Manual::Plugins>.
207 One plugin that is indispensible when using Maypole and the Template View is
208 C<Template::Plugin::Class> -- This allows you to import and use any class installed
209 within a template. For example :
213 [% USE foo = Class('Foo') %]
218 Would do the equivilent of 'use Foo; Foo->bar;' in perl. See L<Template::Plugin::Class>
221 TT Filters process strings or blocks within a template, allowing you to truncate,
222 format, escape or encode trivially. A useful selection is included with Template Toolkit
223 and they can also be found on CPAN or can be written easily.
224 See L<Template::Manual::Filters>.
226 TT Macros allow you to reuse small blocks of content, directives, etc. The MACRO directive
227 allows you to define a directive or directive block which is then evaluated each time the
228 macro is called. Macros can be passed named parameters when called.
230 Once a MACRO is defined within a template or 'include'd template it can be used as if it
231 were a native TT directive. Maypole provides a selection of powerful and useful macros
232 in the templates/ directory of the package and these are used in the beerdb and default
233 templates. See the MACRO section of the L<Template::Manual::Directives> documentation.
235 =head1 MAYPOLE MACROS AND FILTERS
237 Maypole provides a collection of useful and powerful macros...TO DO
243 =head2 finish this documentation