1 package Maypole::View::Base;
3 use UNIVERSAL::moniker;
5 use Maypole::Constants;
7 sub new { bless {}, shift } # By default, do nothing.
10 my ( $self, $r ) = @_;
11 my $root = $r->config->template_root || $r->get_template_root;
16 && File::Spec->catdir( $root, $r->model_class->moniker )
18 File::Spec->catdir( $root, "custom" ),
19 File::Spec->catdir( $root, "factory" )
24 my ( $self, $r ) = @_;
25 my $class = $r->model_class;
26 my $base = $r->config->uri_base;
30 objects => $r->objects,
37 my $classmeta = $args{classmetadata} ||= {};
38 $classmeta->{name} ||= $class;
39 $classmeta->{description} ||= $class->description;
40 $classmeta->{table} ||= $class->table;
41 $classmeta->{columns} ||= [ $class->display_columns ];
42 $classmeta->{list_columns} ||= [ $class->list_columns ];
43 $classmeta->{colnames} ||= { $class->column_names };
44 $classmeta->{related_accessors} ||= [ $class->related($r) ];
45 $classmeta->{moniker} ||= $class->moniker;
46 $classmeta->{plural} ||= $class->plural_moniker;
47 $classmeta->{cgi} ||= { $class->to_cgi };
49 # User-friendliness facility for custom template writers.
50 if ( @{ $r->objects || [] } > 1 ) {
51 $args{ $r->model_class->plural_moniker } = $r->objects;
54 ( $args{ $r->model_class->moniker } ) = @{ $r->objects || [] };
59 local $r->{template_args}{classmetadata}; # already overrides
60 %args = ( %args, %{ $r->{template_args} || {} } );
65 my ( $self, $r ) = @_;
66 $r->{content_type} ||= "text/html";
67 $r->{document_encoding} ||= "utf-8";
68 my $status = $self->template($r);
69 return $self->error($r) if $status != OK;
74 my ( $self, $r, $desc ) = @_;
75 $desc = $desc ? "$desc: " : "";
76 warn $desc . $r->{error} ."\n";
77 if ( $r->{error} =~ /not found$/ ) {
79 # This is a rough test to see whether or not we're a template or
81 return -1 unless @{ $r->{objects} || [] };
85 <H1> Template not found </H1>
87 This template was not found while processing the following request:
89 <B>@{[$r->{action}]}</B> on table <B>@{[ $r->{table} ]}</B> with objects:
92 @{[join "\n", @{$r->{objects}}]}
95 Looking for template <B>@{[$r->{template}]}</B> in paths:
98 @{[ join "\n", $self->paths($r) ]}
101 $r->{content_type} = "text/html";
102 $r->{output} = $r->{error};
105 $r->{content_type} = "text/plain";
106 $r->{output} = $r->{error};
111 sub template { die shift() . " didn't define a decent template method!" }
118 Maypole::View::Base - Base cl
122 This is the base class for Maypole view classes. This is an abstract class
123 meant to define the interface, and can't be used directly.
127 This is the engine of this module. It populates all the relevant variables
128 and calls the requested action.
130 Anyone subclassing this for a different database abstraction mechanism
131 needs to provide the following methods:
135 In this method you do the actual processing of your template. it should use L<paths>
136 to search for components, and provide the templates with easy access to the contents
137 of L<vars>. It should put the result in $r->{output} and return OK if processing was
138 sucessfull, or populate $r->{error} and return ERROR if it fails.
140 =head1 Other overrides
142 Additionally, individual derived model classes may want to override the
146 The default constructor does nothing. You can override this to perform actions
147 during view initialization.
151 Returns search paths for templates. the default method returns factory, custom and
152 <tablename> under the configured template root.
156 returns a hash of data the template should have access to. The default one populates
157 classmetadata if there is a class, as well as setting the data objects by name if
158 there is one or more objects available.