]> git.decadent.org.uk Git - maypole.git/blob - lib/Maypole/View/TT.pm
+ some TT2 options need to be set on the Template::Provider
[maypole.git] / lib / Maypole / View / TT.pm
1 package Maypole::View::TT;
2 use base 'Maypole::View::Base';
3 use Maypole::Constants;
4 use Template;
5 use File::Spec::Functions qw(catdir tmpdir);
6
7 use strict;
8 our $VERSION = "1." . sprintf "%04d", q$Rev$ =~ /: (\d+)/;
9
10 sub template {
11     my ( $self, $r ) = @_;
12
13     unless ($self->{tt}) {
14         my $view_options = $r->config->view_options || {};
15         (my $driver_name = ref $r) =~ s/::/_/g;
16         my $compile_dir  = catdir(tmpdir(), $driver_name);
17         $self->{provider} = Template::Provider->new(
18             COMPILE_DIR    => $compile_dir,
19             %$view_options,
20         );
21         $self->{tt}       = Template->new({
22             COMPILE_DIR    => $compile_dir,
23             %$view_options,
24             LOAD_TEMPLATES => [ $self->{provider} ],
25         });
26     }
27
28     $self->{provider}->include_path([ $self->paths($r) ]);
29
30     my $output;
31     if ($self->{tt}->process( $r->template, { $self->vars($r) }, \$output )) {
32         $r->{output} = $output;
33         return OK;
34     }
35     else {
36         $r->{error} = $self->{tt}->error;
37         return ERROR;
38     }
39 }
40
41 1;
42
43 =head1 NAME
44
45 Maypole::View::TT - A Template Toolkit view class for Maypole
46
47 =head1 SYNOPSIS
48
49     BeerDB->config->view("Maypole::View::TT"); # The default anyway
50
51     # Set some Template Toolkit options
52     BeerDB->config->view_options( {
53         TRIM        => 1,
54         COMPILE_DIR => '/var/tmp/mysite/templates',
55     } );
56
57 =head1 DESCRIPTION
58
59 This is the default view class for Maypole; it uses the Template Toolkit
60 to fill in templates with the objects produced by Maypole's model classes.
61 Please see the Maypole manual, and in particular, the C<View> chapter,
62 for the template variables available and for a refresher on how template
63 components are resolved.
64
65 The underlying Template toolkit object is configured through
66 C<$r-E<gt>config-E<gt>view_options>. See L<Template> for available options.
67
68 =over 4
69
70 =item template
71
72 Processes the template and sets the output. See L<Maypole::View::Base>
73
74 =back
75
76
77 =head1 AUTHOR
78
79 Simon Cozens
80
81 =cut
82