1 package Maypole::View::Base;
3 use UNIVERSAL::moniker;
5 use Maypole::Constants;
8 sub new { bless {}, shift } # By default, do nothing.
11 my ( $self, $r ) = @_;
12 my $root = $r->config->template_root || $r->get_template_root;
13 if(ref($root) ne 'ARRAY') {
17 foreach my $path (@$root) {
22 && File::Spec->catdir( $path, $r->model_class->moniker )
25 push(@output, File::Spec->catdir( $path, "custom" ));
26 push(@output, File::Spec->catdir( $path, "factory" ));
35 my ( $self, $r ) = @_;
36 my $class = $r->model_class;
37 my $base = $r->config->uri_base;
41 objects => $r->objects,
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;
60 # User-friendliness facility for custom template writers.
61 if ( @{ $r->objects || [] } > 1 ) {
62 $args{ $r->model_class->plural_moniker } = $r->objects;
65 ( $args{ $r->model_class->moniker } ) = @{ $r->objects || [] };
70 %args = ( %args, %{ $r->template_args || {} } );
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;
84 my ( $self, $r, $desc ) = @_;
85 $desc = $desc ? "$desc: " : "";
86 carp $desc . $r->{error};
87 if ( $r->{error} =~ /not found$/ ) {
89 # This is a rough test to see whether or not we're a template or
91 return -1 unless @{ $r->{objects} || [] };
95 <H1> Template not found </H1>
97 This template was not found while processing the following request:
99 <B>@{[$r->{action}]}</B> on table <B>@{[ $r->{table} ]}</B> with objects:
102 @{[join "\n", @{$r->{objects}}]}
105 Looking for template <B>@{[$r->{template}]}</B> in paths:
108 @{[ join "\n", $self->paths($r) ]}
111 $r->{content_type} = "text/html";
112 $r->{output} = $r->{error};
115 $r->{content_type} = "text/plain";
116 $r->{output} = $r->{error};
121 sub template { die shift() . " didn't define a decent template method!" }
128 Maypole::View::Base - Base class for view classes
132 This is the base class for Maypole view classes. This is an abstract class
133 that defines the interface, and can't be used directly.
137 This is the entry point for the view. It templates the request and returns a
138 C<Maypole::Constant> indicate success or failure for the view phase.
140 Anyone subclassing this for a different rendering mechanism needs to provide
141 the following methods:
145 In this method you do the actual processing of your template. it should use
146 L<paths> to search for components, and provide the templates with easy access
147 to the contents of L<vars>. It should put the result in C<$r-E<gt>output> and
148 return C<OK> if processing was sucessfull, or populate C<$r-E<gt>error> and
149 return C<ERROR> if it fails.
151 =head1 Other overrides
153 Additionally, individual derived model classes may want to override the
157 The default constructor does nothing. You can override this to perform actions
158 during view initialization.
162 Returns search paths for templates. the default method returns folders for the
163 model class's C<moniker>, factory, custom under the configured template root.
167 returns a hash of data the template should have access to. The default one
168 populates classmetadata if there is a table class, as well as setting the data
169 objects by name if there is one or more objects available.