Home
Home Page
PHP and Web. Caching
Job with Cookies on PHP
Electronic dispatches
JUzabiliti the main page
Natural keys against artificial keys
Uniform autentifikacija Windows NT/2000 and Oracle
The manual on Link Popularity
Partner Links: optimize an exchange of links
What for registration in catalogues through 1PS.RU is necessary
Krossbrauzernyj DHTML
DHTML-skriplet - it is simple about simple
Promotion of a site with the help of bulletin boards
The practical grant{manual} on a spelling of slogans for websites
We check the site - that has taken place with your ranging?
PHP: Patterns
Use of patterns in PHP4
Really easy change of design
Job with patterns, use HTML-Template with CGI-scripts
Job with files in PHP
Links
 

Use of patterns in PHP4

Many programmers faced a problem when " freedom " in a spelling html is necessary for the designer, and to the programmer "cleanliness" of a code:) At me such has happened at a spelling of virtual web - shop. In general, without thinking twice I began to search for various PHP-classes for creation of " dynamic sites " with use of so-called patterns. Also has found one which met all my requirements, but too he appeared " big and heavy ". This class FastTemplate (I do not remember where I have downloaded it ) is called. Without thinking twice, I have decided to write the class, having taken for a basis functionality FastTemplate. My results in a spelling of the class have turned out practically such as at FastTemplate, but as it seems to me, not worse nearly (a comment: I did not copy a code, and created itself from zero).


So, for the beginning jobs it is necessary for you to download my class template.

Have downloaded? Now it is possible to try on a simple example. We shall make an example of listing of files of the current catalogue with calculation kol-va byte of each file, thus dynamically having created the table. So, create the following files:

main.htm


<html>

<head>

<title> <! - ABOUT-> </title>

<link rel = "stylesheet" type = "text/css" href = "/styles.css ">

</head>

<body>


<p align = "center" class = "b"> <!-


ABOUT


-> <br>

Localtime is <! - LOCALTIME->

</p> <br>


<div align = "center"> <table STYLE = "border-collapse:collapse" class = "th">

<tr>

<td colspan = "2" class = "th" align = "center" style = " background-color: * 000000; color:white "> File listing </td>

</tr>

<!-

TABLE_CONTENT

->

<tr>

<td class = "th" align = "right" style = " background-color: * 000000; color:white "> *nbsp; </td>

<td class = "th" style = " background-color: * 000000; color:white "> *nbsp; <! - TOTAL-> *nbsp; </td>

</tr>

</table> </div>


</body>

</html>



rows.htm


<tr>

<td align = "right" class = "th" style = " background-color: <! - COLOR->; color:black "> *nbsp; <! - PWD-> *nbsp; </td>

<td class = "th" style = " background-color: <! - COLOR->; color:black "> *nbsp; <! - FILESIZE-> *nbsp; </td>

</tr>



index.php


<? php


require (' templates.php '); // we Include a class in job with patterns


// We determine tegi

$meta = array ("ABOUT" => ":: template class example:: ",

"LOCALTIME" => date (" M-d-Y H:i:s "));


$t = new template;


// We initialize files and descriptors

$t-> init (array (index => "main.htm", rows => "rows.htm"));


// We establish{install} terminators (delimiter-y)

$t-> delimiters (" <! - ", "-> ");


// We determine tegi

$t-> assign ($meta);


$d = dir(".;


$colors = array ("*d4d4d4", "*a0a0a0"); $i = 0;

$totalbytes = 0;

while ($entry = $d-> read ()) {

if (preg_match (" / ^ (\. |\.\.) $ / ", $entry)) continue;

$color = $colors [$i];

$t-> assign ("FILENAME", $entry); // It is determined under tegom FILNAME a name of a file

$t-> assign ("COLOR", $color); // under COLOR the current color

$t-> assign ("PWD", realpath ($entry)); // the Full way

if (($size = filesize ($entry))> 1024) {

$totalbytes + = $size;

$size = sprintf (" %0.2f Kbytes ", ($size / 1024));

} else {

$totalbytes + = $size;

$size. = "bytes";

}

$t-> assign ("FILESIZE", $size); // we connect with tegom FILESIZE dlinnu a file


// We process a file with a descriptor rows, thus the received results

// We fix under tegom TABLE_CONTENT (by addition)

$t-> parseit (rows, "TABLE_CONTENT");

$i = (++ $i> = count ($colors))? 0: $i;


// Under it tegom we will have the general{common} kol-in byte najdenykh files

$t-> assign ("TOTAL", (($totalbytes> 1024)? sprintf (" %0.2f Kbytes ", ($totalbytes / 1024)): $totalbytes. "bytes"));

}


$d-> close ();


// We process page with a descriptor index. I.e. at processing

// All coming across tegi will be replaced with the certain value.

$t-> parseit (index);


// We deduce{remove} all

$t-> printit ();

$t-> freshall ();


?>



Results of performance it is possible to see here <http://null.magelan.ru/php/templates> the Given class works very much shustro, obrabotchik it is constructed on the basis of regular expressions.

The detailed description


In a class files are certain:

$filelist - an associative file deksriptorov and files

$assign - an associative file certain{determined} tegov

$root - the root

$arr - a file with result

$delmiters - terminators

// init - Initialization of patterns

// arr - an associative file (sm.primer) with descriptors and files

// root - the root where files (to default current) lay

// delimit - here it is possible to specify the terminator from two symbols, for example " {} "

function init ($arr = " ", $root = " ", $delimit = " ")


// Installation of the root of patterns

function setroot ($root = " ")


// Addition of descriptors and files for job with patterns

// list - an associative file

function listit ($list = " ")


// Processing a pattern

// $d - a descriptor of a file OR the FILE!

// $temp - processing in teg $temp (if not to specify, given kh-ija will process

// deksr.fajla also will add it  to a file with results)

function parseit ($d = " ", $temp = " ")


// To receive the processed file, returns string

// $array - a file

function getparsed ($array)


// To specify terminators

// $d1 - left, $d2 - right

// For example $t-> delimiters (" <! - ", "-> ");

function delimiters ($d1 = " ", $d2 = " ")


// As listit, only to add it is possible not an associative file, and

// A simple line

// $d - a descriptor of a file

// $name - a name of a file

function addtolist ($d = " ", $name = " ")


// It is used for clearing all files in a class

function freshall ()


// It is used for clearing results of processing

// It is cleared $arr

function fresh ()


// To display

function printit ()