X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=lib%2FApache%2FMVC%2FView%2FTT.pm;h=babd0f457a9260735ccd438eb88c5b2ce2c00837;hb=4c4e58eb02155a43397f30b900c5b30f755cb874;hp=30e0f89030ca2e606654e9a376d1ff123e24e55e;hpb=cac38e4a644618bbc965d5349723638df1c2e711;p=maypole.git diff --git a/lib/Apache/MVC/View/TT.pm b/lib/Apache/MVC/View/TT.pm index 30e0f89..babd0f4 100644 --- a/lib/Apache/MVC/View/TT.pm +++ b/lib/Apache/MVC/View/TT.pm @@ -1,18 +1,21 @@ package Apache::MVC::View::TT; +use Apache::Constants; use Lingua::EN::Inflect; use Template; use File::Spec; use UNIVERSAL::moniker; +use strict; sub new { bless {}, shift } # Not worth having sub _tt { my ($self, $r) = @_; - my $root = $r->config->{template_root}; + my $root = $r->{ar}->document_root . "/". $r->{ar}->location; + warn "Root was $root"; Template->new({ INCLUDE_PATH => [ $root, - File::Spec->catdir($root, $r->model_class->moniker), + ($r->model_class && File::Spec->catdir($root, $r->model_class->moniker)), File::Spec->catdir($root, "custom"), File::Spec->catdir($root, "factory") ]}); @@ -20,40 +23,60 @@ sub _tt { sub _args { my ($self, $r) = @_; + my $class = $r->model_class; my %args = ( request => $r, - class => $r->model_class, objects => $r->objects, + base => $r->config->{uri_base}, + config => $r->config # ... - ); + ) ; + if ($class) { + $args{classmetadata} = { + name => $class, + columns => [ $class->columns ], + colnames => { $class->column_names }, + related_accessors => [ $class->related($r) ], + 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){ - $args{$r->model_class->plural_moniker} = $r->objects; - } else { - ($args{$r->model_class->moniker}) = @{$r->objects}; + # User-friendliness facility for custom template writers. + if (@{$r->objects || []} > 1) { + $args{$r->model_class->plural_moniker} = $r->objects; + } else { + ($args{$r->model_class->moniker}) = @{$r->objects}; + } } + + # Overrides + %args = (%args, %{$r->{template_args}||{}}); %args; } sub process { my ($self, $r) = @_; - my $template = $self->_tt($r): + my $template = $self->_tt($r); my $output; - $template->process($r->template, { $self->_args($r) }, \$output); - || $self->error($r, $template->error); + $template->process($r->template, { $self->_args($r) }, \$output) + || return $self->error($r, $template->error); + $r->{ar}->content_type("text/html"); $r->{ar}->headers_out->set("Content-Length" => length $output); - $r->send_http_header; - $r->print($output); + $r->{ar}->send_http_header; + $r->{ar}->print($output); return 200; } sub error { my ($self, $r, $error) = @_; - $r->{ar}_>content_type("text/plain"); - $r->send_http_header; - $r->print($error); - return 500; + warn $error; + if ($error =~ /not found$/) { return DECLINED } + $r->{ar}->send_http_header("text/plain"); + $r->{ar}->print($error); exit; } + +1;