]> git.decadent.org.uk Git - maypole.git/blob - lib/Maypole/Manual/View.pod
removed Maypole::Model->description,
[maypole.git] / lib / Maypole / Manual / View.pod
1 =head1 NAME
2
3 Maypole::Manual::View - Maypole View Classes
4
5 =head1 DESCRIPTION
6
7 In a large application, you will almost certainly want to customize the
8 layout and design of the output pages. This task may even be the purview
9 of a separate team of HTML designers rather than the programmers. Since
10 a typical programmer will try to avoid touching HTML as much as possible
11 and a typical designer will try to avoid touching Perl code, programmers
12 have evolved a system of templating to separate the concerns of
13 programming and designing. 
14
15 One of the core concepts in Maypole is the I<view class>, and this is
16 responsible for routing the data produced in the model class into the
17 templates produced by the designers. Of course, there are a great many
18 possible templating systems and styles, and so there can be a great many
19 possible Maypole view classes. Each view class will take the data from
20 the controller, locate a template to be processed, and hand the whole
21 lot to its preferred templating module, which will then do the hard work
22 of filling in the template and coming up with the output.
23
24 You can choose whatever Maypole view class you want, but the default
25 view class is L<Maypole::View::TT>, and it feeds its data and templates
26 to a module called the Template Toolkit.
27
28 =head2 The Template Toolkit
29
30 The Template Toolkit, written by Andy Wardley, is a very powerful and
31 generic templating system. It provides its own little formatting language
32 which supports loops, conditionals, hash and array dereferences and
33 method calls, macro processing and a plug-in system to connect it to
34 external Perl modules.
35 Its homepage is C<http://www.template-toolkit.org/>.
36 There are several good introductions to the
37 Template Toolkit available: you should have one installed as
38 L<Template::Tutorial::Datafile>; there's one at
39 L<http://www.perl.com/pub/a/2003/07/15/nocode.html>, and of course
40 there's the "Badger Book" - I<The Perl Template Toolkit>, by Andy et al.
41 C<http://www.oreilly.com/catalog/perltt/index.html>
42
43 We'll present a brief introduction here by deconstructing some of the 
44 templates used by Maypole applications. For more deconstruction, see
45 L<Standard Templates and Actions|Maypole::Manual::StandardTemplates>,
46 which is an entire chapter dealing with the
47 factory supplied templates.
48
49 Here's a template that could be called for the front page of the example
50 beer database application, C<custom/frontpage>.
51
52     [% INCLUDE header %]
53
54     <h2> The beer database </h2>
55
56     <TABLE BORDER="0" ALIGN="center" WIDTH="70%">
57     [% FOR table = config.display_tables %]
58     <TR>
59     <TD>
60     <A HREF="[%table%]/list">List by [%table %]</A>
61     </TD>
62     </TR>
63     [% END %]
64     </TABLE>
65
66 The first thing to note about this is that everything outside of the
67 Template Toolkit tags (C<[%> and C<%]>) is output verbatim. That is,
68 somewhere in the output you're guaranteed to see 
69
70     <h2> The beer database </h2>
71
72     <TABLE BORDER="0" ALIGN="center" WIDTH="70%">
73
74 Inside the tags, magic happens. The first piece
75 of magic is the C<[% INCLUDE header %]> directive. This goes away and
76 finds a file called F<header> - don't worry about how it finds that yet,
77 we'll come to that later on - and processes the file's contents as
78 though they were right there in the template. Our F<header> file happens
79 not to contain any C<[% %]> tags, but if it did, they would be processed
80 in the same way as the ones in F<frontpage>. 
81
82 The next piece of magic is this line:
83
84     [% FOR table = config.display_tables %]
85
86 We're seeing a lot of things here at once. C<config> is where we should
87 start looking. This is a template variable, which is what templates are
88 all about - templating means getting data from somewhere outside and
89 presenting it to the user in a useful way, and C<config> is a
90 prime example of data that we want to use. It's actually an object
91 containing configuration parameters for this Maypole application, and
92 one of the methods is C<display_tables>, which returns a list of the
93 database tables that we're supposed to show. In the application, we
94 probably said something like
95
96     BeerDB->config->display_tables([qw[beer brewery pub style]]);
97
98 This stores the four values - C<beer>, C<brewery>, C<pub> and C<style> -
99 in an array, which is placed in the config object using the
100 accessor/mutator method C<display_tables>. Now we're getting them back
101 again. Note that we're not going to show the handpump table.
102
103 The Template Toolkit's dot operator is a sort of do-the-right-thing
104 operator; we can say C<array.0> to get the first element of an array,
105 C<hash.key> to look up the C<key> key in a hash, and C<object.method> to
106 call C<method> on an object. So, for instance, if we said
107 C<config.display_tables.2>, we'd look up the C<display_tables> method in
108 the configuration object and get our array back, then look up the 3rd
109 element and get C<pub>.
110 Thing is, you don't have to care whether C<display_tables> is an object
111 or a hash. You can pretend it's a hash if you want. The syntax is the
112 same, and Template Toolkit knows the right thing to do.
113
114 The C<FOR> loop will repeat the code four times, setting our new
115 variable C<table> to the appropriate array element. This code:
116
117     [% FOR table = config.display_tables %]
118         Hello [% table %]!
119     [% END %]
120
121 will produce something like
122
123     Hello beer!
124     Hello brewery!
125     Hello pub!
126     Hello style!
127
128 In our case, though, we're printing out a table element linking to each
129 database table in turn.
130
131 Here's a slightly more complicated example, adapted from F<factory/pager>.
132 This template is responsible for printing the little page menu at the
133 bottom of a listing if there are more rows in the listing than we want
134 on a single page.
135
136     [% PROCESS macros %]
137     <P ALIGN="center">Pages:
138     [%
139          FOREACH num = [pager.first_page .. pager.last_page];
140               IF num == pager.current_page;
141                 "["; num; "] ";
142               ELSE;
143                 SET args = "?page=" _ num;
144                 SET label = "[" _ num _ "]";
145                 link(classmetadata.table, "list", args, label);
146               END;
147          END;
148     %]
149     </P>
150
151 Maypole will be providing a whole bunch of variables to this template,
152 and we'll look at them all in a moment, but the only ones we need to care
153 about are C<pager> and C<classmetadata>. 
154
155 We start by loading in a bunch of macros. Macros are Template Toolkit's
156 functions - you can provide them some parameters and they'll run a little
157 sub-template based on them. The C<macros> file contains some handy macros
158 that I've found useful for constructing Maypole templates; again, these
159 will be covered in full detail in
160 L<Standard Templates and Actions|Maypole::Manual::StandardTemplates>.
161
162 We're going to be displaying something like this:
163
164     Pages: [1] [2] [3] [4]
165
166 with most of those numbers being a link to the appropriate page. This
167 mean we're going to have to have a list of numbers, and the C<FOREACH> loop
168 provides this: (C<FOREACH> and C<FOR> are identical, just like in Perl.)
169
170          FOREACH num = [pager.first_page .. pager.last_page];
171
172 Here we're manually constructing an array of numbers, using the range
173 operator (C<..>) to fill in all the numbers from the C<first_page> (1)
174 to the C<last_page> (4). The same dot operator is used to ask the C<pager>
175 object what its C<first_page> and C<last_page> are.
176
177 Now we're going to be executing this loop four times, once each for C<num>
178 being set to 1, 2, 3, and 4. At some point, we'll come across the page
179 that we're actually on right now:
180
181       IF num == pager.current_page;
182
183 and in that case, we don't want to produce a link to it. We just want
184 to output it as text, surrounded by square brackets:
185
186                 "["; num; "] ";
187
188 We're using string literals to output the brackets. We don't have to
189 do that. We could say it this way:
190
191     [% ...
192       IF num == pager.current_page;
193     %]
194         [ [% num %] ] 
195     [% ELSE %]
196        ...
197     [% END %]
198
199 But you know, I quite like it my way.
200
201 Now if the number we're printing isn't the number of the current page,
202 we want to make a link. Here's how we do it:
203
204     SET args = "?page=" _ num;
205     SET label = "[" _ num _ "]";
206     link(classmetadata.table, "list", args, label);
207
208 C<SET> declares a new variable of our own. If there was anything called
209 C<args> before, there isn't now. It's going to be the result of our
210 statement C<"?page=" _ num>. C<_> is the concatenation operator, and
211 glues C<?page=> onto the front of our number. So if we want to link to
212 page 4, then the C<args> variable will contain C<?page=4>. Similarly,
213 the C<label> variable will be C<[4]>.
214
215 Now we call a macro, C<link> with these two variables and the value of
216 C<classmetadata.table>. This macro takes four arguments, C<table>, 
217 C<action>, C<args> and C<label>, and constructs a link of the form
218
219     <A HREF="[% base %]/[% table %]/[% action %][% args %]">
220     [% label %]
221     </A>
222
223 In our case, it'll be filled in like so:
224
225     <A HREF="[% base %]/[% classmetadata.table %]/list?page=4">
226     [ 4 ]
227     </A>
228
229 Where C<classmetadata.table> will actually be the name of the current
230 table, and C<base> will be replaced by the appropriate URL for
231 this application.
232
233 =head2 Locating Templates
234
235 Another feature of C<Maypole::View::TT> which may not be present in
236 alternate view class implementations - although they are strongly
237 encouraged to provide it - is the way that templates are located.
238 (Remember, I B<did> say I'd tell you about that later.) Template Toolkit
239 allows whatever uses it to provide a path for template files to be
240 located in. C<Maypole::View::TT> feeds it up to three possible
241 directories to look things up in, and it will try to find a template in
242 each of these in turn.
243
244 When you configure a Maypole application, you can tell it the base
245 directory of your templates like so:
246
247     BeerDB->config->template_root("/var/www/beerdb/templates");
248
249 If you don't do this, most Maypole front-ends will use the current
250 directory, which may be what you want anyway. Off this directory,
251 Maypole will look for a set of subdirectories.
252
253 For instance, I said we were in the middle of processing the front page
254 and looking up a template file called F<header>. Maypole will first look
255 for this file in the F<custom> subdirectory. (say,
256 F</var/www/beerdb/templates/custom>) If it doesn't find one, then it
257 looks in the F<factory> subdirectory. If it doesn't find one there, then
258 it gives up and dies with an error. But that's your fault, since you've
259 called for a template which doesn't exist. Don't do that. 
260
261 This behaviour means that you can provide your own site-specific
262 templates, but if you don't do so, then you get to use a generic one
263 provided by Maypole. Maypole's "factory setting" templates are written
264 in such a way as to try and do the right thing no matter what your
265 application does. They are occasionally successful at this. 
266
267 Now the front page was a pretty simple example, since Maypole only looks
268 up two directories. In most cases, it checks an additional directory,
269 and this directory depends entirely on what Maypole is doing.
270
271 If you're writing an e-commerce application, for example, you may well
272 have a table which represents the product catalogue and all the products
273 you can buy. Let's call this the C<product> table. You'll also have a
274 data source which is specific to the user which contains all the
275 products that they're buying on this particular visit to the site. In
276 time-honoured tradition, we'll call this the C<basket> table.
277
278 Now it ought to be reasonably apparent that you don't want the basket
279 to be displayed in exactly the same way as the product catalogue. The
280 templates for C<product/list> and C<basket/list> need to be different.
281 This is where the third directory comes in. The other directory, which
282 Maypole checks very first of all, is specific to the table that you're
283 viewing. So if you go to C<http://your.shop.com/basket/list>, Maypole
284 will look in the F<basket> directory first for a file called F<list>,
285 and second in the F<custom> directory for a site-wide list template,
286 and then fall-back to the F<factory> directory for a generic list
287 template. It should be obvious that you probably want to provide all
288 of F<basket/list>, F<basket/view>, F<product/list>, F<product/view>
289 and any other combination of classes and actions that you can think of.
290
291 =head2 What Maypole provides to a template
292
293 C<Maypole::View::TT> provides quite a variety of template variables to
294 the template. As these are the building blocks of your pages, it's worth
295 looking at precisely what variables are available.
296
297 =head3 objects
298
299 The most important variable is called C<objects>, and is a list of all
300 the objects that this page is going to deal with. For instance,
301 if the URL is C<http://localhost/beerdb/beer/view/23>, then
302 in the template F</beer/view>, C<objects> will contain the C<BeerDB::Beer>
303 object for the 23rd item in the database, while for the F</brewery/list>
304 template, the view will fill C<objects> with all the breweries; or at
305 least, all the breweries on the current page.
306
307 =head3 breweries!
308
309 This variable is so important that to help design templates with it,
310 C<Maypole::View::TT> provides a helpful alias to it depending on
311 context. For instance, if you're writing your own F</brewery/list>
312 template, the data in C<objects> is also available in a template
313 variable called C<breweries>. If you're working on F</brewery/view>,
314 though, it's available in C<brewery>, since there's only one brewery to
315 be displayed.
316
317 =head3 base
318
319 Additionally, you can get the base URL for the application from the
320 C<base> template variable; this allows you to construct links, as we
321 saw earlier:
322
323     <A HREF="[% base %]/brewery/edit/[% brewery.id %]">Edit this brewery</A>
324
325 =head3 config
326
327 You can also get at the rest of the configuration for the site with the
328 C<config> variable as we saw above.
329
330 =head3 request
331
332 The entire request object is made available in 
333 C<request>, should you really need to poke at it. (I've only found this
334 useful when working with authentication modules which stash a current user
335 object in C<request.user>.)
336
337 =head3 classmetadata
338
339 To allow the construction of the "generic" templates which live in
340 F<factory>, Maypole also passes in a hash called C<classmetadata>,
341 which contains all sorts of useful information about the class under
342 examination:
343
344 =over 3
345
346 =item C<table>
347
348 This is the name of the table that is represented by the class.
349
350 =item C<name>
351
352 This is the Perl's idea of the class; you don't need this unless you're
353 doing really tricky things.
354
355 =item C<moniker>
356
357 This is a more human-readable version of the table name, that can be
358 used for display. "brewery" for example.
359
360 =item C<plural>
361
362 The same, but a correctly-formed plural. For instance, "breweries".
363
364 =item C<columns>
365
366 The list of columns for display; see the
367 L<hard way|Maypole::Manual::Beer/"The hard way"> section in the Beer
368 Database chapter.
369
370 =item C<list_columns>
371
372 As for C<columns>, but these are the columns to be displayed on a
373 F<list> page.
374
375 =item C<colnames>
376
377 This is a hash mapping the database's name for a column to a more
378 human-readable name. Again, see "Customizing Generic CRUD Applications".
379
380 =item C<cgi>
381
382 This is a slightly trickier one. It is a hash mapping column names to
383 a C<HTML::Element> suitable for entering data into a new instance of
384 that class. That is, for the C<beer> table, C<classmetadata.cgi.style>
385 should be a C<HTML::Element> object containing a drop-down list of
386 beer styles.
387
388 =item C<related_accessors>
389
390 This is a list of accessors which can be called on an object to get
391 lists of other things that this object "has". For instance, on a
392 brewery, it would return C<beers>, since calling C<brewery.beers> would
393 give you a list of beers produced by the brewery. Note that this only
394 caters for accessors defining one-to-many relationships, not the
395 ordinary one-to-one relationships, such as C<style>.
396
397 =back
398
399 =head3 Additional variables and overrides
400
401 You can pass additional data to templates by creating new variables.
402 You'd typically do this in your view class.
403 Just add the name of your template variable as a key to the
404 C<template_args> hash in the request object, and supply its value:
405
406   $r->template_args->{your_variable_name} = 'some_value';
407
408 You can also override the value of any of the standard variables by
409 giving their name as the key.
410
411 =head2 Other view classes
412
413 Please note that these template variables, C<config>, C<classmetadata>,
414 C<objects> and its user-friendly alias, as well as the rest of them are
415 a function of one particular view class, the default
416 C<Maypole::View::TT> class. Other view classes may need to present an
417 entirely different set of template variables, since the default ones
418 might not make sense. The templates may look wildly different in other
419 view class implementations. But that's OK, because you couldn't
420 necessarily use the same templates with a different templating system
421 anyway.
422
423 For instance, in really dumb templating languages which can't handle
424 dereferencing hashes or arrays - no wait, that's most of them - passing
425 in a hash reference like C<classmetadata> won't help you since you can't
426 get at any of its elements. So you'll need to take a look at the
427 documentation for the appropriate view class to see what template
428 variables it provides.
429
430 So if, for some perverse reason, the Template Toolkit just isn't good
431 enough for you, then you can set your own view class while configuring
432 your application:
433
434    package BeerDB;
435    use base Maypole::Application;
436    ...
437    BeerDB->setup("dbi:SQLite:t/beerdb.db");
438    BeerDB->config->uri_base(http://localhost/beerdb/");
439    BeerDB->config->rows_per_page(10);
440    BeerDB->config->view("Maypole::View::Mason"); 
441
442 Where do these alternate view classes come from? Gentle reader, they
443 come from B<you>.
444
445 =head2 Building your own view class
446
447 I<You should probably skip this section for the first few readings of this manual. It's only intended for people extending Maypole.>
448
449 Imagine you've found a brand new templating system that's B<much better>
450 than the Template Toolkit. I know I'm stretching your imagination a bit
451 here, but try. You'd like to use it with Maypole, which means writing your
452 own view class. How is it done?
453
454 We'll demonstrate by implementing a view class for L<HTML::Mason>,
455 although no value judgement is implied. C<HTML::Mason> is a templating
456 system which embeds pure Perl code inside its magic tags. The good side
457 of this is that it can get into hash references and objects, and so
458 providing C<classmetadata>, C<config> and the Maypole request object
459 will work out just fine. The down side is that C<HTML::Mason> is used to
460 running more or less standalone, and having all the template variables
461 it wants already at its disposal through CGI parameters and the like, so
462 we have to fiddle a bit to get these variables into our template.
463
464 The key to building view classes is L<Maypole::View::Base>. This is the
465 base class that you're going to inherit from and, to be honest, it does
466 pretty much everything you need. It provides a method called C<vars>
467 which returns a hash of all the template variables described above, so
468 it would be good to feed those into C<HTML::Mason>. It also provides a
469 C<paths> method which turns returns the full filesystem path of the
470 three possible template paths as shown above. Again, it would be good to
471 use this as our component paths if we can. It also has some methods we
472 can override if we want to, but they're not massively important, so you
473 can see L<Maypole::View::Base> for more about them. 
474
475 The module will do the right thing for us if we agree to provide a
476 method called C<template>. This is responsible for taking the Maypole
477 request object C<$r> (of which more later) and putting the appropriate output
478 either into C<$r-E<gt>output> or C<$r-E<gt>error>, depending, of
479 course, whether things are OK or whether we got an error.
480
481 Thankfully, C<HTML::Mason> makes things really easy for us. We B<can>
482 use multiple template roots, so we can use the C<paths> method; we
483 B<can> pass in a hash full of interesting data structures, so we can use
484 the C<vars> method too. In fact, we have to do very little to make
485 C<Maypole::View::Mason> work. Which is somewhat annoying, because it
486 makes a boring example. But it means I can leave the fun ones to you!
487
488 The doing-the-templating, in Mason and in any templating system, depends on
489 three things: the paths that we're going to use to find our templates, the
490 template name that we've been asked to fill out, and the set of variables that
491 are going to be fed to the template. We'll assemble these for reference:
492
493     sub template {
494         my ($self, $r) = @_;
495         my @paths = $self->paths($r);
496         my $template = $r->template;
497         my %vars = $self->args($r);
498
499 We'll also declare somewhere to temporarily store the output:
500
501         my $output;
502
503 Now comes the part where we have to actually do something
504 templating-language specific, so we open up our copy of "Embedding Perl
505 in HTML with Mason" and find the bit where it talks about running Mason
506 standalone. We find that the first thing we need to do is create a
507 C<HTML::Mason::Interp> object which knows about the component roots.
508 There's a slight subtlety in that the component roots have to be
509 specified as an array of arrays, with each array being a two-element
510 list of label and path, like so:
511
512     comproot => [
513         [ class   => "/var/www/beerdb/templates/brewery" ],
514         [ custom  => "/var/www/beerdb/templates/custom" ],
515         [ factory => "/var/www/beerdb/templates/factory" ],
516     ]
517
518 We also find that we can set the output method here to capture Mason's
519 output into a scalar, and also that we can tell Mason to generate
520 sensible error messages itself, which saves us from having to worry
521 about catching errors. At the end of all this, we come up with a
522 constructor for our C<HTML::Mason::Interp> object which looks like this:
523
524     my $label = "path0";
525     my $mason = HTML::Mason::Interp->new(
526         comproot => [ map { [ $label++ => $_ ] } @paths ],
527         output_method => \$output,
528         error_mode => "output" 
529     );
530
531 The next thing we need to do is run the template with the appropriate
532 template variables. This turns out to be really easy:
533
534     $mason->exec($template, %vars);
535
536 Now we've got the data in C<$output>, we can put it into the request object,
537 and return a true value to indicate that we processed everything OK. (If there
538 was an error, then Mason will have produced some suitable output, so we can
539 pretend that everything's OK anyway.)
540
541     $r->output($output);
542     return 1;
543
544 And that's all we need to do. Barely twenty lines of code for the finished
545 product. Wasn't that easy? Don't you feel inspired to write Maypole view
546 classes for your favourite templating language? Well, don't let me stop you!
547 Patches are always welcome!
548
549 =head2 Links
550
551 L<Contents|Maypole::Manual>,
552 Next L<Standard Templates and Actions|Maypole::Manual::StandardTemplates>,
553 Previous L<Maypole Model Classes|Maypole::Manual::Model>,