Creation of a site
In general, to program for a Mason-cursor it is a pleasure: time of development of routine things is reduced, allowing will be concentrated on the basic programming.
Creation of design we shall lower{omit} and we shall pass to a program part. As the design of a site will be identical to all pages, we shall allocate static and dynamic parts. In the elementary case it is three elements: the top part of page (header), a body of page (body) and the bottom part (footer) - see fig. 1. Header and footer (a static part of a site) will be the general{common} for all pages. Therefore they can be born{be taken out} in separate components and to connect in all pages. But to register the same code in all components (in a dynamic part) - employment{occupation} ungrateful{thankless}. Programmers on (PHP/ASP) bypass this inconvenience, organizing a conclusion of all contents of a site from one page. The transmitted parameter, defines{determines}, it is necessary to give out what document. But it not the best variant, and at complex{difficult} structure of a site generates a number{line} of problems.
Mason offers more convenient way of detour of the given inconvenience - through a component autohandler. Mason before processing of the called component checks in current and overlying directories presence of a file autohandler. If he is found, management all over again is passed him. So, there has come time of a spelling of the first Mason-document. A file autohandler:
<html>
<title> Test page </title>
<style type = "text/css">
<!--
body {background-color: <% $BG %>}
->
</style>
<body text = <% $FN %>>
<table width=800 align=center border=0 cellpadding=0 cellspacing=0>
<tr> <td align=right colspan=5> <% scalar localtime () %> </td> </tr>
<tr> <td colspan=5> Test page </td> </tr>
<tr> <td width=150 valign=top>
<! - the Left column->
</td>
<td width=1 background = "img/bg.jpg">
<img src = "img/bg.jpg" border=0>
</td>
<td width=498>
<! - the Body of the document->
<% $m-> call_next %>
<! - the End of a body of the document->
</td>
<td width=1 background = "img/bg.jpg">
<img src = "img/bg.jpg" border=0>
</td>
<td width=150 valign=top> <br>
<! - the Right menu->
</td>
</tr>
</table> </body>
</html>
<%init>
my $BG;
my $FN;
$BG = $cookie {'BG'} || " *006633 ";
$FN = $cookie {'FN'} || " *cccc66 ";
</%init>
For economy of a place of the description of styles are lowered{omitted}. The full version of a test site can be taken to the address [4]. In autohandler are both header and footer. We shall understand, as all works. Let the file index.html. Mason all over again is requested finds autohandler, carries out the block <%init> and gives out to the user everything, that precedes $m-> call_next. It also is ours header. The method call_next passes management to the requested component (index.html). After processing and delivery of results of job index.html the rest autohandler is deduced.
Aspiring to make a site maximum convenient, we shall give to the visitor an opportunity to adjust color of the text and a background (though parameters it is possible to choose more, but for demonstration shall be limited to two). You, probably, have noticed global khehsh %cookie. This khehsh contains transferred{handed} by a browser cookies. we shall not go into details yet entering cookies in this khehsh.
Thus, if from the user are sent cookies default values are replaced user. Thanking autohandler, we will be not not disturbed any more with a headache under the account of client adjustments and a conclusion of a static part of a site.
Now it is possible to concentrate on programming of more important things. Further we shall consider a file index.html (fig. 2). We admit{allow}, he deduces the last 10 news, sorting on decrease of dates. All news are stored{kept} in MySQL in the table news.
<table>
% while (my ($date, $desrc, $link) = $select-> fetchrow ()) {
<tr> <td>
<% $date %> - <% $descr %>... <a href = " <than % $link %> "> is more detailed </a>
</td> </tr>
%}
</table>
<%init>
my $select = $dbh-> prepare (qq (SELECT * FROM news ORDER BY ndate DESC LIMIT 10));
$select-> execute;
</%init>
<%cleanup>
$select-> finish;
</%cleanup>
The author not the supporter of to store{keep} a SQL-code in components. At change of the table it is possible to not recollect simply, in what documents sample is carried out. An optimum variant can be creation of the separate module with constants or use of own procedures.
In the beginning of clause{article} it was spoken, that module Mason allows to make changes in obrabotchik, to be exact to use the. If there is an opportunity, to use it{him} better. In the distribution kit of the module there is "skeleton" of it obrabotchika - a file handler.pl. Certainly, you should not write from zero the handler. It is enough to add only the code in already available. In handler.pl it is connected HTML:: Mason, he and performs all job. In this file are created global khehsh %cookie and object $dbh. do not forget to replace line PerlModule HTML:: Mason:: ApacheHandler on PerlRequire/path/handler.pl in httpd.conf.
In handler.pl there are two blocks to which it is possible to make changes. In the first modules are connected. It is very convenient to connect all used modules in this block and to work with them in components through global objects. In the second (the method handler) is described all code. An example of a file
handler.pl:
package HTML:: Mason;
use HTML:: Mason;
use HTML:: Mason:: ApacheHandler;
use strict;
{
* The block of connection of modules
package HTML:: Mason:: Commands;
* Global variables
use vars qw (%cookie $dbh);
* Used modules
use CGI:: Cookie;
use DBI;
}
* Creation of objects mason
my $parser = new HTML:: Mason:: Parser;
my $interp = new HTML:: Mason:: Interp (parser => $parser, comp_root => '/usr/local/www/mason ', data_dir => '/usr/local/www/data ');
my $ah = new HTML:: Mason:: ApacheHandler (interp => $interp);
chown (Apache-> server-> uid, Apache-> server-> gid, $interp-> files_written);
sub handler
{
my ($r) = _;
my %c = parse CGI:: Cookie ($r-> header_in ('Cookie'));
* We delete pred``yduhhee value
%HTML:: Mason:: Commands:: cookie = ();
foreach my $temp (keys %c) {
* We add values in khehsh
$HTML:: Mason:: Commands:: cookie {$temp} = $c {$temp}-> value ();
}
* We are connected to a DB
$HTML:: Mason:: Commands:: dbh = DBI-> connect (" DBI:mysql:database=mason; host=host ", "user", "pass");
my $status = $ah-> handle_request ($r);
$HTML:: Mason:: Commands:: dbh-> disconnect ();
return $status;
}
The component with the form of adjustment of design of a site looks as follows.
<%def .outmesg>
<% $ARGS {'MESG'} %>
</%def>

|