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 fill
62 in templates with the objects produced by Maypole's model classes. Please see
63 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
82 L<Template::Manual::Directives>.
84 A simple example would be :
90 Dear [% title %] [% surname %],
91 Thank you for your letter dated [% your.date %]. This is to
92 confirm that we have received it and will respond with a more
93 detailed response as soon as possible. In the mean time, we
94 enclose more details of ...
98 TT uses '[%' and '%]' (by default) to delimit directives within a template, and
99 the simple directives above just display the value of variable named within
100 those delimiters -- [% title %] will be replaced inline with the value of the
101 'title' variable passed in the 'stash' to the template when it is processed.
103 You can access nested data through the dot ('.') operator, which will
104 dereference array or hash elements, but can also be used to call methods on
105 objects, i.e. '[% name.salutation("Dear %s,") %]'. The other main operator is
106 underscore ('_'), which will concatonate strings or variables.
108 The value returned by a directive replaces the directive inline when the
109 template is processes, you can also SET a value which will not return anything,
110 or CALL a method or operation which will also not return anything.
112 You can specify expressions using the logical (and, or, not, ?:) and mathematic
113 operators (+ - * / % mod div).
117 [% template.title or default.title %]
121 [% order.nitems ? checkout(order.total) : 'no items' %]
125 TT allows you to include or re-use templates through it's INCLUDE, PROCESS and
126 INSERT directives, which are fairly self explainatory. You can also re-use parts
127 of template with the BLOCK or MACRO directives.
129 Conditional and Looping constructs are simple and powerful, and TT provides an
130 inbuilt iterator and helper functions and classes that make life sweet.
132 Conditional directives are IF, UNLESS, ELSIF, ELSE and behave as they would in
138 Hello [% name %], does your mother know you're using her AOL account?
140 Sorry, you're not old enough to enter (and too dumb to lie about your age)
145 [% UNLESS text_mode %] [% INCLUDE biglogo %] [% END %]
149 Looping directives are FOREACH, LAST and BREAK.
151 FOREACH loops through a HASH or ARRAY processing the enclosed block for each
154 Looping through an array
156 [% FOREACH i = items %]
160 Looping through a hash
162 [% FOREACH u IN users %]
163 * [% u.key %] : [% u.value %]
166 Looping through an array of hashes
168 [% FOREACH user IN userlist %]
169 * [% user.id %] [% user.name %]
172 The LAST and BREAK directive can be used to exit the loop.
174 The FOREACH directive is implemented using the Template::Iterator module. A
175 reference to the iterator object for a FOREACH directive is implicitly available
176 in the 'loop' variable. The loop iterator object provides a selection of methods
177 including size(), max(), first(), last(), count(), etc
181 [% FOREACH item IN [ 'foo', 'bar', 'baz' ] -%]
182 [%- "<ul>\n" IF loop.first %]
183 <li>[% loop.count %]/[% loop.size %]: [% item %]
184 [%- "</ul>\n" IF loop.last %]
189 See Template::Iterator for further details on looping and the Iterator.
191 You might notice the minus ('-') operator in the example above, it is used to
192 remove a newline before or after a directive so that you can layout the Template
193 logic as above but the resulting output will look exactly how you require it.
195 You will also frequently see comments and multi-line directives, # at the start
196 of a directive marks it as a comment, i.e. '[%# this is a comment %]'. A
197 multiline directive looks like :
203 You can see that lines are terminated with a semi-colon (';') unless the
204 delimter ('%]') closes the directive.
206 For full details of the Template Toolkit see Template::Manual and
207 Template::Manual::Directives, you can also check the website, mailing list or
208 the Template Toolkit book published by O Reilly.
210 =head1 TEMPLATE PLUGINS, FILTERS AND MACROS
212 The Template Toolkit has a popular and powerful selection of Plugins and
215 TT Plugins provide additional functionality within Templates, from accessing CGI
216 and databases directly, handling paging or simple integration with Class::DBI
217 (for those rare occasions where you don't actually need Maypole). See
218 L<Template::Manual::Plugins>.
220 One plugin that is indispensible when using Maypole and the Template View is
221 C<Template::Plugin::Class> -- This allows you to import and use any class
222 installed within a template. For example :
226 [% USE foo = Class('Foo') %]
231 Would do the equivilent of 'use Foo; Foo->bar;' in perl. See
232 L<Template::Plugin::Class> for details.
234 TT Filters process strings or blocks within a template, allowing you to
235 truncate, format, escape or encode trivially. A useful selection is included
236 with Template Toolkit and they can also be found on CPAN or can be written
237 easily. See L<Template::Manual::Filters>.
239 TT Macros allow you to reuse small blocks of content, directives, etc. The MACRO
240 directive allows you to define a directive or directive block which is then
241 evaluated each time the macro is called. Macros can be passed named parameters
244 Once a MACRO is defined within a template or 'include'd template it can be used
245 as if it were a native TT directive. Maypole provides a selection of powerful
246 and useful macros in the templates/ directory of the package and these are used
247 in the beerdb and default templates. See the MACRO section of the
248 L<Template::Manual::Directives> documentation.
250 =head1 MAYPOLE MACROS AND FILTERS
252 Maypole provides a collection of useful and powerful macros...TO DO
258 =head2 finish this documentation