]> git.decadent.org.uk Git - maypole.git/blob - lib/Maypole/View/Base.pm
improvement to error reporting for template problems [bug 14133] in Maypole::View...
[maypole.git] / lib / Maypole / View / Base.pm
1 package Maypole::View::Base;
2 use File::Spec;
3 use UNIVERSAL::moniker;
4 use strict;
5 use Maypole::Constants;
6 use Carp;
7
8 sub new { bless {}, shift }    # By default, do nothing.
9
10 sub paths {
11     my ( $self, $r ) = @_;
12     my $root = $r->config->template_root || $r->get_template_root;
13     if(ref($root) ne 'ARRAY') {
14         $root = [ $root ];
15     }
16     my @output = ();
17     foreach my $path (@$root) {
18         push(@output, $path);
19         push(@output,
20              (
21               $r->model_class
22               && File::Spec->catdir( $path, $r->model_class->moniker )
23               )
24              );
25         push(@output, File::Spec->catdir( $path, "custom" ));
26         push(@output, File::Spec->catdir( $path, "factory" ));
27     }
28     return @output;
29 }
30
31
32
33
34 sub vars {
35     my ( $self, $r ) = @_;
36     my $class = $r->model_class;
37     my $base  = $r->config->uri_base;
38     $base =~ s/\/+$//;
39     my %args = (
40         request => $r,
41         objects => $r->objects,
42         base    => $base,
43         config  => $r->config
44
45           # ...
46     );
47     if ($class) {
48         my $classmeta = $r->template_args->{classmetadata} ||= {};
49         $classmeta->{name}              ||= $class;
50         $classmeta->{table}             ||= $class->table;
51         $classmeta->{columns}           ||= [ $class->display_columns ];
52         $classmeta->{list_columns}      ||= [ $class->list_columns ];
53         $classmeta->{colnames}          ||= { $class->column_names };
54         $classmeta->{related_accessors} ||= [ $class->related($r) ];
55         $classmeta->{moniker}           ||= $class->moniker;
56         $classmeta->{plural}            ||= $class->plural_moniker;
57         $classmeta->{cgi}               ||= { $class->to_cgi };
58         $classmeta->{stringify_column}  ||= $class->stringify_column;
59
60         # User-friendliness facility for custom template writers.
61         if ( @{ $r->objects || [] } > 1 ) {
62             $args{ $r->model_class->plural_moniker } = $r->objects;
63         }
64         else {
65             ( $args{ $r->model_class->moniker } ) = @{ $r->objects || [] };
66         }
67     }
68
69     # Overrides
70     %args = ( %args, %{ $r->template_args || {} } );
71     %args;
72 }
73
74 sub process {
75     my ( $self, $r ) = @_;
76     $r->{content_type}      ||= "text/html";
77     $r->{document_encoding} ||= "utf-8";
78     my $status = $self->template($r);
79     return $self->error($r) if $status != OK;
80     return OK;
81 }
82
83 sub error {
84     my ( $self, $r, $desc ) = @_;
85     $desc = $desc ? "$desc: " : "";
86     carp $desc . $r->{error};
87     if ( $r->{error} =~ /not found$/ ) {
88
89         # This is a rough test to see whether or not we're a template or
90         # a static page
91         return -1 unless @{ $r->{objects} || [] };
92
93         my $template_error = $r->{error};
94         $r->{error} = <<EOF;
95
96 <h1> Template not found </h1>
97
98 A template was not found while processing the following request:
99
100 <strong>@{[$r->{action}]}</strong> on table
101 <strong>@{[ $r->{table} ]}</strong> with objects:
102
103 <pre>
104 @{[join "\n", @{$r->{objects}}]}
105 </pre>
106
107
108 The main template is <strong>@{[$r->{template}]}</strong>.
109 The template subsystem's error message was
110 <pre>
111 $template_error
112 </pre>
113 We looked in paths:
114
115 <pre>
116 @{[ join "\n", $self->paths($r) ]}
117 </pre>
118 EOF
119         $r->{content_type} = "text/html";
120         $r->{output}       = $r->{error};
121         return OK;
122     }
123     $r->{content_type} = "text/plain";
124     $r->{output}       = $r->{error};
125     $r->send_output;
126     return ERROR;
127 }
128
129 sub template { die shift() . " didn't define a decent template method!" }
130
131 1;
132
133
134 =head1 NAME
135
136 Maypole::View::Base - Base class for view classes
137
138 =head1 DESCRIPTION
139
140 This is the base class for Maypole view classes. This is an abstract class
141 that defines the interface, and can't be used directly.
142
143 =head2 process
144
145 This is the entry point for the view. It templates the request and returns a
146 C<Maypole::Constant> indicate success or failure for the view phase.
147
148 Anyone subclassing this for a different rendering mechanism needs to provide
149 the following methods:
150
151 =head2 template
152
153 In this method you do the actual processing of your template. it should use
154 L<paths> to search for components, and provide the templates with easy access
155 to the contents of L<vars>. It should put the result in C<$r-E<gt>output> and
156 return C<OK> if processing was sucessfull, or populate C<$r-E<gt>error> and
157 return C<ERROR> if it fails.
158
159 =head1 Other overrides
160
161 Additionally, individual derived model classes may want to override the
162
163 =head2 new
164
165 The default constructor does nothing. You can override this to perform actions
166 during view initialization.
167
168 =head2 paths
169
170 Returns search paths for templates. the default method returns folders for the
171 model class's C<moniker>, factory, custom under the configured template root.
172
173 =head2 vars
174
175 returns a hash of data the template should have access to. The default one
176 populates classmetadata if there is a table class, as well as setting the data
177 objects by name if there is one or more objects available.
178
179 =head2 error
180
181
182 =cut