X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=lib%2FMaypole%2FManual%2FStandardTemplates.pod;h=dbf127998c9963c88cf6363942c3339b01ecf6b5;hb=8b68aa68ac5e83bc439d16c1fda0fb3870ee246c;hp=e5eb9f06b2f32aeaebde9438469bb90301b9b2d1;hpb=616082000cbe7305fbfc84d33bb2f9d53cc86f3c;p=maypole.git diff --git a/lib/Maypole/Manual/StandardTemplates.pod b/lib/Maypole/Manual/StandardTemplates.pod index e5eb9f0..dbf1279 100644 --- a/lib/Maypole/Manual/StandardTemplates.pod +++ b/lib/Maypole/Manual/StandardTemplates.pod @@ -1,7 +1,12 @@ -=head1 Maypole's Standard Templates and Actions +=head1 NAME -As we saw in our CRUD example, Maypole does all it can to make your life +Maypole::Manual::StandardTemplates - Maypole's Standard Templates and Actions + +=head1 DESCRIPTION + +As we saw in our Create-Read-Update-Delete (CRUD) example, +Maypole does all it can to make your life easier; this inclues providing a set of default actions and factory-supplied templates. These are written in such a generic way, making extensive use of class metadata, that they are more or less @@ -12,8 +17,18 @@ actions do their stuff, and how the default templates are put together. Once we have an understanding of what Maypole does for us automatically, we can begin to customize and create our own templates and actions. +Although the standard templates can be applied in many situations, +they're really provided just as examples, +as a starting point to create your own templates to suit your needs. +The goal of templating is to keep templates simple so the presentation +can be changed easily when you desire. +We're not trying to build a single set of reusable templates that cover +every possible situation. + =head2 The standard actions +Remember that actions are just subroutines in the model classes with an +I attribute. A simple, uncustomized Maypole model class, such as one of the classes in the beer database application, provides the following default actions - that is, provides access to the following URLs: @@ -32,9 +47,11 @@ web form to edit the object; it submits to C. =item C +When called with an ID, the C action provides row editing. + =item C -This provides both editing and row creation facilities. +When called without an ID, the C action provides row creation. =item C @@ -61,7 +78,7 @@ into an object, and hand it to the template to be displayed. However, as taking the first argument and turning it into an object is such a common action, it is handled directly by the model class's C method. Similarly, the default template name provided by the C method -is the name of the acction, and so will be C or C +is the name of the action, and so will be C or C accordingly. So the code required to make these two actions work turns out to be: @@ -76,7 +93,7 @@ separated out the concerns of "acting" and displaying. Both of these "actions" are purely concerned with displaying a record, and don't need to do any "acting". Remember that the "edit" method doesn't actually do any editing - this is provided by C; it is just another view of -the data, albeit once which allows the data to be modified later. These +the data, albeit one which allows the data to be modified later. These two methods don't need to modify the row in any way, they don't need to do anything clever. They just are. @@ -109,7 +126,7 @@ mode, acting upon that object. If not, we're in create mode. sub do_edit :Exported { my ($self, $r) = @_; - my $h = CGI::Untaint->new(%{$r->{params}}); + my $h = CGI::Untaint->new(%{$r->params}); my ($obj) = @{$r->objects || []}; if ($obj) { # We have something to edit @@ -118,15 +135,42 @@ mode, acting upon that object. If not, we're in create mode. $obj = $self->create_from_cgi($h); } -The C model uses L to turn C parameters -into database table data. This in turn uses C to ensure +The C model uses the C and C +methods of L to turn C parameters +into database table data. This in turn uses L to ensure that the data coming in is suitable for the table. If you're using the default C model, then, you're going to need to set up your tables in a way that makes C happy. -=over +The data is untainted, and any errors are collected into a hash which is +passed to the template. We also pass back in the parameters, so that the +template can re-fill the form fields with the original values. The user +is then sent back to the C template. + + if (my %errors = $obj->cgi_update_errors) { + # Set it up as it was: + $r->template_args->{cgi_params} = $r->params; + $r->template_args->{errors} = \%errors; + $r->template("edit"); + } + +Otherwise, the user is taken back to viewing the new object: + + } else { + $r->template("view"); + } + $r->objects([ $obj ]); + +Notice that this does use hard-coded names for the templates to go to next. +Feel free to override this in your subclasses: + + sub do_edit :Exported { + my ($class, $r) = @_; + $class->SUPER::do_edit($r); + $r->template("my_edit"); + } -=item Digression on C +=head3 Digression on C C is a mechanism for testing that incoming form data conforms to various properties. For instance, given a C @@ -167,36 +211,6 @@ This is usually integer, if you're using numeric IDs for your primary key. If not, you probably want C, but you probably know what you're doing anyway. -=back - -The data is untainted, and any errors are collected into a hash which is -passed to the template. We also pass back in the parameters, so that the -template can re-fill the form fields with the original values. The user -is then sent back to the C template. - - if (my %errors = $obj->cgi_update_errors) { - # Set it up as it was: - $r->{template_args}{cgi_params} = $r->{params}; - $r->{template_args}{errors} = \%errors; - $r->{template} = "edit"; - } - -Otherwise, the user is taken back to viewing the new object: - - } else { - $r->{template} = "view"; - } - $r->objects([ $obj ]); - -Notice that this does use hard-coded names for the templates to go to next. -Feel free to override this in your subclasses: - - sub do_edit :Exported { - my ($class, $r) = @_; - $class->SUPER::do_edit($r); - $r->template("my_edit"); - } - =head3 delete The delete method takes a number of arguments and deletes those rows from the @@ -218,8 +232,8 @@ URL, we want, by default, all the records in the table: However, things are slightly complicated by paging and ordering by column; the default implementation also provides a C object to the templates and uses that to retrieve the appropriate bit of -the data, as specified by the C URL query parameter. See the F -template below. +the data, as specified by the C URL query parameter. See the +L<"pager"> template below. =head3 search @@ -252,14 +266,16 @@ macros as we come across them. =head3 F -=template view +template view =head3 F -The F template is pretty much the same as F, but it uses the +The F template is pretty much the same as F, but it uses +L's C method on each column of an object to return a C object representing a form element to edit that property. These elements -are then rendered to HTML with C. It expects to see a list of +are then rendered to HTML with C or to XHTML with C. +It expects to see a list of editing errors, if any, in the C template variable: FOR col = classmetadata.columns; @@ -285,4 +301,69 @@ The C template argument is used to distinguish between the two cases:

Listing of all [% classmetadata.plural %]

[% END %] -=head1 Customizing Generic CRUD Applications +=head3 F + +The pager template controls the list of pages at the bottom (by default) +of the list and search views. It expects a C template argument +which responds to the L interface. +There's a description of how it works in +L +of the View chapter. + +=head3 F + +The F template is included at the start of most other templates +and makes some generally-useful template macros available: + +=over + +=item C + +This makes an HTML link pointing to C +labelled by the text in I