]> git.decadent.org.uk Git - maypole.git/blob - lib/Maypole/View/TT.pm
Add in "table" to the class metadata.
[maypole.git] / lib / Maypole / View / TT.pm
1 package Maypole::View::TT;
2 use Lingua::EN::Inflect;
3 use Template;
4 use File::Spec;
5 use UNIVERSAL::moniker;
6 use strict;
7
8
9 sub new { bless {}, shift } # Not worth having
10
11 sub _tt {
12     my ($self, $r) = @_;
13     # This bit sucks.
14     my $root = $r->{config}{template_root} || $r->get_template_root;
15     Template->new({ INCLUDE_PATH => [
16         $root,
17         ($r->model_class && File::Spec->catdir($root, $r->model_class->moniker)),
18         File::Spec->catdir($root, "custom"),
19         File::Spec->catdir($root, "factory")
20     ]});
21 }
22
23 sub _args {
24     my ($self, $r) = @_;
25     my $class = $r->model_class;
26     my %args = (
27         request => $r,
28         objects => $r->objects,
29         base    => $r->config->{uri_base},
30         config  => $r->config
31         # ...
32     ) ;
33     if ($class) { 
34         $args{classmetadata} = {
35             name => $class,
36             table => $class->table,
37             columns => [ $class->display_columns ],
38             colnames => { $class->column_names },
39             related_accessors => [ $class->related($r) ],
40             moniker => $class->moniker,
41             plural  => $class->plural_moniker,
42             cgi => { $class->to_cgi },
43             description => $class->description
44         };
45
46         # User-friendliness facility for custom template writers.
47         if (@{$r->objects || []} > 1) { 
48             $args{$r->model_class->plural_moniker} = $r->objects;
49         } else {
50             ($args{$r->model_class->moniker}) = @{$r->objects ||[]};
51         }
52     }
53
54     # Overrides
55     %args = (%args, %{$r->{template_args}||{}});
56     %args;
57 }
58
59 sub process {
60     my ($self, $r) = @_;
61     my $template = $self->_tt($r);
62     my $output;
63     $template->process($r->template, { $self->_args($r) }, \$output)
64     || return $self->error($r, $template->error);
65
66     $r->{content_type} ||= "text/html";
67     $r->{output} = $output;
68     return 200;
69 }
70
71 sub error {
72     my ($self, $r, $error) = @_;
73     warn $error;
74     if ($error =~ /not found$/) { return -1 }
75     $r->{content_type} = "text/plain";
76     $r->{output} = $error;
77     $r->send_output;
78     exit;
79 }
80
81 1;