Realization of patterns on PHP.
The introduction.
Good afternoon dear sirs! In given clause{article} I would like to mention very important subject, namely patterns in PHP. In this clause{article} I shall result idle time, but a working example of "patterns", also we shall consider all pro and contra use of patterns.
Use of patterns.
Before to use patterns, think, whether it is valid they to you so are necessary? At present there is a huge quantity{amount} of commercial variants of patterns. All of them work by one principle (value, replacement), but have huge quantity{amount} navorotov, such as automatic changes of the register of variables, search on regular expressions, etc., all this is certainly good and is easily sold. When I have decided to see a "commercial" pattern, I uzhasnulsja, his one class weighed 398 KB. It is normal? Also in a network it is possible to find set of free-of-charge variants of patterns (classes of patterns in PHPBB, IPB …), but all of them weigh much and work not too quickly. I offer you a simple skeleton of "patterns" on PHP, with his help it is possible to make class shablonizator, with all functions necessary for you.
Pro and contra.
I shall result to you a vital example, not so long ago I was engaged in development of the program for one person, beforehand have been discussed, that I write the program, and design his this business. After a while, my customer writes to me, that design for my program to make it is impossible. Certainly, the person of nothing knowing in web-programming will experience huge difficulties, at construction of design in the PHP-program. The main task of ' patterns ' is to facilitate a life to the designer. Certainly, it is possible to count main plus of use of patterns that the designer without the help of the programmer can change the web-project. Also I like division - the program and design.
I do not use patterns in the personal projects since they give additional "loading". Patterns it is good, but to use them it is necessary only if write what the public project or you execute job to order.
Realization of patterns on PHP.
And so we shall start. In total we will have 2 key files.
1) file2compile.tpl - a file which we shall be parsit`
2) template.php - the main file a containing class of patterns
Listing of a file file2compile.tpl:
<html>
<head> {TITLE} </head>
<body bgcolor = {BGCOLOR}>
{SOMETPLTAGS}
</body>
</html>
Listing of a file template.php:
<? php
class parse_class
{
var $vars = array ();
var $template;
function get_tpl ($tpl_name)
{
if (empty ($tpl_name) ||! file_exists ($tpl_name))
{
return false;
}
else
{
$this-> template = file_get_contents ($tpl_name);
}
}
function set_tpl ($key, $var)
{
$this-> vars [$key] = $var;
}
function tpl_parse ()
{
foreach ($this-> vars as $find => $replace)
{
$this-> template = str_replace ($find, $replace, $this-> template);
}
}
}
$parse = new parse_class;
?>
Now I shall in detail describe the maintenance{contents} of these two files.
File: file2compile.tpl
Here it is resulted usual HTML a code. In the given file it is possible to find variables of a kind {TITLE}. These are those variables which we shall just replace with the value necessary to us.
File: template.php
We have PHP a class divided{shared} into 3 functions. Right at the beginning of a file we declare class variables.
$vars - a file with values (a variable, replacement).
$template - a file which we shall be parsit`.
Now we shall pass to the description of functions.
Function: get_tpl
As argument function accepts a name of a file. Whether in a body of function we check the argument is set and whether there is a file. If the argument is not set also a file there are no we return value FALSE. Otherwise we fill a class variable (template) with the maintenance{contents} of a file.
Function set_tpl
Function accepts 2 values, it is a variable (napr. {TITLE}} and on which we her shall replace value.
Function tpl_parse
Function does not accept any values. In a body of function we reads out a file $vars and we make replacement of the established variables by preset values.
Use of a class.
For a conclusion to the screen use the following commands:
<? php
require (' template.php '); // we Connect a file with a class
$parse-> get_tpl (' template.tpl '); // the File which we shall be parsit`
$parse-> set_tpl (' {TITLE} ',' Super a site '); // Installation of a variable {TITLE}
$parse-> set_tpl (' {BGCOLOR} ',' *F2F2F2 '); // Installation of a variable {BGCOLOR}
$parse-> set_tpl (' {SOMETPLTAGS} ',' <font color=red> It it is the text obramlenyj red color </font> '); // Installation of a variable {SOMETPLTAGS}
$parse-> tpl_parse (); // Parsim
print $parse-> template; // It Is deduced{removed} our page
?>

|