]> git.decadent.org.uk Git - maypole.git/blob - lib/Apache/MVC/View/TT.pm
9cdc0dfef450b147b4fadaa276e76379ff880230
[maypole.git] / lib / Apache / MVC / View / TT.pm
1 package Apache::MVC::View::TT;
2 use Lingua::EN::Inflect;
3 use Template;
4 use File::Spec;
5 use UNIVERSAL::moniker;
6
7
8 sub new { bless {}, shift } # Not worth having
9
10 sub _tt {
11     my ($self, $r) = @_;
12     my $root = $r->config->{template_root};
13     Template->new({ INCLUDE_PATH => [
14         $root,
15         File::Spec->catdir($root, $r->model_class->moniker),
16         File::Spec->catdir($root, "custom"),
17         File::Spec->catdir($root, "factory")
18     ]});
19 }
20
21 sub _args {
22     my ($self, $r) = @_;
23     my %args = (
24         request => $r,
25         class   => $r->model_class,
26         objects => $r->objects,
27         # ...
28     );
29
30     # User-friendliness facility for custom template writers.
31     if (@{$r->objects} > 1){
32         $args{$r->model_class->plural_moniker} = $r->objects;
33     } else {
34         ($args{$r->model_class->moniker}) = @{$r->objects};
35     }
36     %args;
37 }
38
39 sub process {
40     my ($self, $r) = @_;
41     my $template = $self->_tt($r);
42     my $output;
43     $template->process($r->template, { $self->_args($r) }, \$output)
44     || $self->error($r, $template->error);
45     $r->{ar}->content_type("text/html");
46     $r->{ar}->headers_out->set("Content-Length" => length $output);
47     $r->send_http_header;
48     $r->print($output);
49     return 200;
50 }
51
52 sub error {
53     my ($self, $r, $error) = @_;
54     $r->{ar}->content_type("text/plain");
55     $r->send_http_header;
56     $r->print($error);
57     return 500;
58     exit;
59 }