]> git.decadent.org.uk Git - maypole.git/blobdiff - lib/Apache/MVC/View/TT.pm
Location handling stuff.
[maypole.git] / lib / Apache / MVC / View / TT.pm
index 5c67d502a9d17a0d095df6fdaba5d8c477787fce..368c3ecc330546b01b42466788825ade02f16ddd 100644 (file)
@@ -3,26 +3,41 @@ use Lingua::EN::Inflect;
 use Template;
 use File::Spec;
 use UNIVERSAL::moniker;
+use strict;
 
-sub template_root { "/opt/houseshare/templates" } # For now
 
 sub new { bless {}, shift } # Not worth having
 
-sub process {
+sub _tt {
     my ($self, $r) = @_;
-    my $root = $self->template_root;
-    my $template = Template->new({ INCLUDE_PATH => [
+    my $root = $r->config->{template_root};
+    Template->new({ INCLUDE_PATH => [
         $root,
         File::Spec->catdir($root, $r->model_class->moniker),
         File::Spec->catdir($root, "custom"),
         File::Spec->catdir($root, "factory")
     ]});
+}
+
+sub _args {
+    my ($self, $r) = @_;
+    my $class = $r->model_class;
     my %args = (
         request => $r,
-        class   => $r->model_class,
+        class   => $class,
         objects => $r->objects,
+        base    => $r->config->{uri_base},
         # ...
     );
+    $args{classmetadata} = {
+        name => $class,
+        columns => [ $class->columns ],
+        colnames => { $class->column_names },
+        moniker => $class->moniker,
+        plural  => $class->plural_moniker,
+        cgi => { $class->to_cgi },
+        description => $class->description
+    };
 
     # User-friendliness facility for custom template writers.
     if (@{$r->objects} > 1){
@@ -30,6 +45,25 @@ sub process {
     } else {
         ($args{$r->model_class->moniker}) = @{$r->objects};
     }
+    %args;
+}
+
+sub process {
+    my ($self, $r) = @_;
+    my $template = $self->_tt($r);
+    my $output;
+    $template->process($r->template, { $self->_args($r) }, \$output)
+    || $self->error($r, $template->error);
+    $r->{ar}->content_type("text/html");
+    $r->{ar}->headers_out->set("Content-Length" => length $output);
+    $r->{ar}->send_http_header;
+    $r->{ar}->print($output);
+    return 200;
+}
 
-    $template->process($r->template, \%args);
+sub error {
+    my ($self, $r, $error) = @_;
+    $r->{ar}->send_http_header("text/plain");
+    $r->{ar}->print($error);
+    exit;
 }