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
 

Job with patterns, use HTML-Template with CGI-scripts

One of often problems{tasks} facing to the Web-programmer is the branch of logic of data acquisition from a format of data presentation. For the decision of such problem  often use patterns of html-documents which at job of programs are filled by the data. It allows to carry out imposition of pages to other person, or to change appearance of pages not changing the program.


CPAN <http://www.cpan.org/> gives the programmer a choice of different means of job with patterns. Among them: HTML:: Template, Text:: Template, Template toolkit. As a rule the technique of job with all these modules is identical. The programmer creates the object specifying the necessary pattern, then the data set is formed, for filling a pattern and filling a pattern is carried out, and the result of it comes back as ready HTML pages. Let's more in detail consider module HTML:: Template.


As against other modules, this module is focused that as patterns pages with HTML a marking will be used. Accordingly, directives of a pattern look similarly to HTML to tags, not preventing HTML to editors to work with a marking of page. With the help of these directives it is possible to fill in a pattern the different data, depending on conditional flags; to simulate cycles, for example at filling tables. Thus all designs can be used inside each other, creating patterns of any complexity.


HTML:: Template also allows the programmer to supervise job with patterns. For example, at use of the given module in a combination with mod_perl it is possible to use caching of the disassembled patterns and to use results in different scripts. Except for caching the programmer can set filters which will make processing the generated text. All parameters of patterns are set at creation of the object describing a pattern.

Creation of object


For job with a pattern it is necessary to create all over again object which describes the chosen pattern. Creation of object is carried out with the help of a call of the operator new. Thus the pattern can be stored{kept} as in a file (a key filename, the most often application), in a scalar variable (the key scalarref, thus the link to object is passed) and in a file of lines (the key arrayref, the link to the list also is used). Except for the listed keys, can be used and additional which provide more exact adjustment of behaviour of object.


The example of creation of object from a pattern taking place in a file template.tmpl, thus is set, that names of patterns will be sensitive to the register of letters:

my $template = HTML:: Template-> new (filename => ' template.tmpl ', case_sensitive => 1);



If in system the variable of an environment $HTML_TEMPLATE_ROOT is established, and at a pattern is set by a relative name of a file value will be added to a name $HTML_TEMPLATE_ROOT.

Reception of result


For reception of the filled pattern simply call function output for the necessary object. Thus the generated text comes back. Thus the status of object of a pattern does not vary.


As very much often the result of job enters the name in a file to reduce loading, you with the help of parameter print_to can pass functions output a descriptor of a file in which there will be a delivery of results. For example, so:

$template-> output (print_to => *STDOUT);



Insert of variables


For an insert of the data in a pattern it is necessary to fill in it  with the help of function param. param value is passed function khesh the data as a key =>. For complex  structures of the data, for example for the enclosed cycles, value can be links to lists and others kheshi. An example of simple use of the module:

se HTML:: Template;

* We open a file with a pattern

my $template = HTML:: Template-> new (filename => ' test.tmpl ');

* We fill a pattern in parameters

$template-> param (HOME => $ENV {HOME}, PATH => $ENV);

* We send heading Content-Type

print " Content-Type: text/html\n\n ";

* We give out ready result

print $template-> output;



lja uses in a pattern of a variable with name VARNAME the design <TMPL_VAR NAME = "VARNAME"> is used. Value placed in khesh with the help of function param is located on a place of the given tag. Thus at us the document not fully complying standard HTML turns out. If you worry about correctness of syntax of your document instead of the given tag it is possible to use the following form: <! - TMPL_VAR NAME=PARAM1->. She works also, as well as previous, but does not break correctness HTML of the document. Both these forms can be used inside other tags, for example so: <IMG SRC = " <TMPL_VAR NAME=IMAGE_SRC> ">.


Processing of deduced{removed} variables can be supervised by parameter of a tag with name ESCAPE. Allowable values are HTML and URL, thus there is a transformation of the symbols which have been not resolved{allowed} in the given context.

Conditional operators


Other often used design is the condition. It has the form <TMPL_IF NAME = "IMJA_UPRAVLJAJUHHEGO_PARAMETRA"> Given 1 <TMPL_ELSE> the Data 2 </TMPL_IF>. Thus the Data 1 will appear in result only if the variable with name IMJA_UPRAVLJAJUHHEGO_PARAMETRA will have true value, otherwise in result the Data 2 will appear. The module supposes use only one branch of a condition, not using <TMPL_ELSE>. Filling of a variable occurs as usually.


Opposite on action to a tag <TMPL_IF..> is teg <TMPL_UNLESS>. He has syntax <TMPL_UNLESS NAME = "IMJA_UPRAVLJAJUHHEGO_PARAMETRA"> the Data </TMPL_UNLESS> and inserts the Data only if variable IMJA_UPRAVLJAJUHHEGO_PARAMETRA has false value or is not determined at all.


Function param can be used also for reception of value of the specified parameter. For this purpose simply call function with a name of the necessary parameter.

The operator of a cycle


The important tag is teg, the designating cycle. He has sleduhhij syntax: <TMPL_LOOP NAME = "LOOP_NAME"> the Repeating part </TMPL_LOOP>. Variable LOOP_NAME should be the link to the list of the data. In this list can it is stored{kept} and it is simple values, and links to others kheshi the data that allows to build the enclosed designs as in the following example.


If we in our pattern shall write for example such design

<table width = " 100 % ">

<TMPL_LOOP NAME=EMPLOYEE_INFO>

    <tr>

      <td> the Name: <TMPL_VAR NAME=NAME> </td>

      <td> the Post: <TMPL_VAR NAME=JOB> </td>

    </tr>

</TMPL_LOOP>

</table>



In a script we shall fill in this pattern the data:

$template-> param (EMPLOYEE_INFO => [

              {name => ' Ivanov ', job => ' the programmer '},

              {name => ' Petrov ', job => ' the manager '},

]);



That we shall receive the following conclusion (only a generated part of a pattern):

<table width = " 100 % ">

    <tr>

      <td> the Name: Ivanov </td>

      <td> the Post: the programmer </td>

    </tr>

    <tr>

      <td> the Name: Petrov </td>

      <td> the Post: the manager </td>

    </tr>

</table>



It is important to remember, that inside a cycle variables used in him are visible only. To refer on external (in relation to a cycle) variables, you should use parameter global_vars at creation of object.

Other operators


The module also allows to include other patterns that helps to use repeating parts in different documents in a pattern. For inclusion of a file filename.tmpl the design <TMPL_INCLUDE NAME = "filename.tmpl"> is used. Thus the pattern can switch on itself and we shall receive infinite rekursiju. Be cautious! At creation of object it is possible to specify what maximal depth of the enclosed files can be achieved. It is supervised kljuchem max_includes.

The conclusion


More detailed information on adjustments of parameters of a conclusion of results and functions look help page HTML:: Template.