]> git.decadent.org.uk Git - maypole.git/blob - lib/Apache/MVC/View/TT.pm
This gives us non-object-based templated pages. And a little start on
[maypole.git] / lib / Apache / MVC / View / TT.pm
1 package Apache::MVC::View::TT;
2 use Apache::Constants;
3 use Lingua::EN::Inflect;
4 use Template;
5 use File::Spec;
6 use UNIVERSAL::moniker;
7 use strict;
8
9
10 sub new { bless {}, shift } # Not worth having
11
12 sub _tt {
13     my ($self, $r) = @_;
14     my $root = $r->{ar}->document_root . "/". $r->{ar}->location;
15     warn "Root was $root";
16     Template->new({ INCLUDE_PATH => [
17         $root,
18         ($r->model_class && File::Spec->catdir($root, $r->model_class->moniker)),
19         File::Spec->catdir($root, "custom"),
20         File::Spec->catdir($root, "factory")
21     ]});
22 }
23
24 sub _args {
25     my ($self, $r) = @_;
26     my $class = $r->model_class;
27     my %args = (
28         request => $r,
29         class   => $class,
30         objects => $r->objects,
31         base    => $r->config->{uri_base},
32         config  => $r->config
33         # ...
34     ) ;
35     if ($class) { 
36         $args{classmetadata} = {
37             name => $class,
38             columns => [ $class->columns ],
39             colnames => { $class->column_names },
40             related_accessors => [ $class->related($r) ],
41             moniker => $class->moniker,
42             plural  => $class->plural_moniker,
43             cgi => { $class->to_cgi },
44             description => $class->description
45         };
46
47         # User-friendliness facility for custom template writers.
48         if (@{$r->objects || []} > 1) { 
49             $args{$r->model_class->plural_moniker} = $r->objects;
50         } else {
51             ($args{$r->model_class->moniker}) = @{$r->objects};
52         }
53     }
54
55     # Overrides
56     %args = (%args, %{$r->{template_args}||{}});
57     %args;
58 }
59
60 sub process {
61     my ($self, $r) = @_;
62     my $template = $self->_tt($r);
63     my $output;
64     warn "Processing ".$r->template;
65     $template->process($r->template, { $self->_args($r) }, \$output)
66     || return $self->error($r, $template->error);
67
68     warn "And off it goes!\n";
69     $r->{ar}->content_type("text/html");
70     $r->{ar}->headers_out->set("Content-Length" => length $output);
71     $r->{ar}->send_http_header;
72     $r->{ar}->print($output);
73     return 200;
74 }
75
76 sub error {
77     my ($self, $r, $error) = @_;
78     warn $error;
79     if ($error =~ /not found$/) { return DECLINED }
80     $r->{ar}->send_http_header("text/plain");
81     $r->{ar}->print($error);
82     exit;
83 }
84
85 1;