PHP: Patterns
Division of registration and the maintenance{contents} - an immemorial problem of the web - developer. For the holder of a small site in some pages of such problem does not arise. Change of design, or other editing of appearance of pages for him does not represent difficulties. However for large webs - resources, portals mixture of these two major components of a site: registrations and maintenances{contents} - it is simply invalid. Otherwise the site becomes so slow, that the owner of a resource loses above it management.
Having collided{faced} once with such problem, sajtostroiteli have started to puzzle over that how to make so that the information did not become attached nikoem in the image to design and on the contrary that appearance of all site varied literally one movement of a hand. It is necessary to say, that a way of ideal division of registration and the maintenance{contents} today it is not found yet. But nevertheless the certain operating time and technologies which allow to come nearer to this ideal maximum are created. First of all it is technology of use of patterns.
What there is a pattern?
Concept "pattern" rather extensive. But more often the pattern is understood as a certain document (usually in format HTML), containing in itself all registration of a resource, or only his parts, and also special labels which pages during generation are replaced with details.
For job with patterns special webs - applications (if to speak about webs - development) which can possess various properties and functions are used. But more often their role is reduced replacing the above-stated labels with information blocks and to connect the received parts of pages in a single whole.
Probably, in all languages vebprogrammirovanija there are similar tools for job with patterns: in Perl it FastTemplate (which, by the way, exists and for PHP, but his use is not recommended as versions comprehensible to job concern to third version PHP), in attitude{relation} PHP is large and multipurpose Smarty, and also a number{line} of other fineer "shablonizatorov".
Smarty
Smarty - one of the largest development of a similar sort. He is realized, as well as practically all applications of similar type, as a class.
This "shablonizator" has huge quantity{amount} of opportunities: he not only carries out replacement of the selected sites of a pattern with the specified data, but also gives use inside a pattern of executed sites, functions, configuration files and other.
To describe all his opportunities and a rule ispol`zvovanija it is not meaningful, as on it one is required not and not two lessons, besides to it rather good documentation is written, and examples of use are applied to itself Smarty.
Smarty is the present{true} monster on job with patterns. But, as understand, it is necessary to pay for so wide functionality resources of the server and time of execution{performance} of a script on his basis. In some cases because of bulkiness Smarty it seems slow. Therefore for rather small projects use Smarty - is usually far from being the best choice. In this case it is necessary to turn the look on fineer and less functional, but nevertheless worthy tools.
Other tools for job with patterns
Here I want to pay your attention on "shablonizatory" on the basis of function eval (). This function regards a code set in its{her} argument as code PHP and, accordingly, executes it . For example:
$text = ' Hello, $name $fam! ';
$name = "Artem";
$fam = "Akatov";
eval (' echo " '. $text. ' "; ');
This primitive example of job with a mini-pattern will deduce{remove} " Hello, Artem Akatov! ".
Advantage of the given type of tools of job with patterns is connected to that, that from the developer beforehand it is not required to define{determine} the text or other information for labels in a pattern (words serve in our example as those labels $name and $fam). Also such "shablonizatory" considerably win on speed.
Let's develop an example above and we shall write function which will take a pattern from a file:
function getTemplate ($template, $ext = "htm") {
$templatefolder = "templates"; // a folder for storage of patterns
return str_replace ("\" "," \ " ", implode (" ", file ($templatefolder. "/". $ template.". $ ext)));
}
$name = "Artem";
$fam = "Akatov";
eval (' echo " '.getTemplate ('test'). ' "; ');
If in a file test.htm we shall place the text from a variable $text from an example above the result vypolenija the given example will be similar previous.
For convenience of job with the given type of patterns classes are written. One of the most successful such classes is Eugeny Kucherova's class.
Let's try to create with his help one of variants of primitive "cursor" for the given unit "Lessons". Thus we shall use recordings MySQL which we have brought at the last lessons.
First of all we shall prepare a pattern. For this purpose we shall open a window with a HTML-code of the given page and we shall replace number{room} of a lesson with a variable $row [id], instead of the name "Patterns" we shall insert $row [title], the text from the first word "Division" up to the last " meetings! " We shall replace on $row [body]. Instead of elements of navigation " Back, on a lesson 16 " and in a place where should settle down " Further, on a lesson 18 ", accordingly we put $prev and $next. We shall save it as page.tpl. The ready pattern can be downloaded here.
Now we work with PHP
We write:
require " class. Template.php "; // a relative way up to a class
$tpl = new Template;
$id = (int) getenv ("QUERY_STRING"); // we take number{room} of a lesson from a line of search
mysql_connect ("host", "artem", "12345");
mysql_select_db ("myphp");
$query = " SELECT * FROM lessons WHERE id = ' $id ' ";
$result = mysql_query ($query);
$row = mysql_fetch_array ($result);
if ($id> 1) $prev = " <a href = '? ". ($ id - 1). " '> Back, on a lesson ". ($id - 1). " </a> ";
$query = " SELECT COUNT (*) FROM lessons ";
$result = mysql_query ($query);
$max = mysql_result ($result, 0);
if ($id <$max) $next = " <a href = '? ". ($ id + 1). " '> is Farther, on a lesson ". ($id + 1). " </a> ";
eval (' echo " '. $ tpl-> get ("page") '. "; ');
It is a simple example of the page having simple structure. But even on such example it is visible, as patterns simplify creation of webs - applications.
In summary
Certainly, here we have disassembled only tiny part of this extensive subject of job with patterns and have considered only small share of tools of job with patterns. But it would be desirable to say, that is not too important, how you work with patterns and you use what means, it is much more important how you with them work. To learn competent job with patterns not so it is simple, the understanding of it comes only with practice, and here I cannot give the certain advice{councils}.
On it all. Up to the following meeting!

|