Generally speaking, the template allows you to complete your PHP code separated from the HTML, the HTML that makes graphics designers very happy, but you can avoid losing them to engage in a valuable design.
It is not FastTemplates
So, we really need to PHPBuilder on templates on a separate article? Well, yes, because there are more than one way to achieve with PHP template. Sascha articles on how to use FastTEmplates, but the basic class library PHP ( "PHPLIB") has its own template is
Now.
What is the difference between them? » FastTemplates Perl was originally from a change to the library. FastTemplates Perl program on the work of the good, but not ideal for PHP. Kristian Koehntopp PHPLIB prepared a template from scratch, as a pure PHP library, it provides a better PHP advantage. Kristian is one of the benefits of the design of the preg_replace () to analyze the template, said the use of the FastTemplate than ereg_replace () faster. Another advantage of PHPLIB template is that it allows the block to achieve dynamic nested, unlike FastTemplates.
phpsu.com is a free phpscool
The two have a very similar characteristics and capabilities, if you have already used the FastTemplates, and you want to learn to use PHPLIB template, you should you know about FastTemplates forget everything. Their identity may be similar, but by the templates PHPLIB everything than FastTemplates only a little bit different.
PHPLIB use templates
Let us a simple example from the start. We assume that the / home / mydir / mytemplates / Below is a group called MyTemplate template, it has some text, which may be:
Congratulations! You win a (some_color) Honda Prelude!
Note "(some_color)" is surrounded by large brackets. Some_color specified in the brackets is a template variables. We may want to write such a script, it can be put into templates, in (some_color) template variables into the local PHP variables $ my_color value, and then export the new text. If the $ my_color happen to be set to "blue", the final output might be:
http://www.phpsu.com
Congratulations! You won a new blue Honda Prelude!
Below are the results of the PHP script: <? Php
include "template.inc";
$ my_color = "blue";
/ / Will be back in use
$ t = new Template ( "/ home / mydir / mytemplates /");
/ / Create a template called target $ t
$ t-> set_file ( "MyFileHandle", "MyTemplate.ihtml");
/ / Set MyFileHandle = our template file
$ t-> set_var ( "some_color", $ my_color);
/ / Set the template variables some_color = $ my_color value
$ t-> parse ( "MyOutput", "MyFileHandle");
/ / Set the template variables MyOutput = analysis of the document
$ t-> p ( "MyOutput");
/ / MyOutput output value (our analysis of the data)
?>
The first line is a include instructions, used to provide PHPLIB template functions. Of course than PHPLIB template to do, but if you just want to use the template of only need to include tmplate.inc (template.inc from PHPLIB is one of the paper). PHPLIB template to use object-oriented programming, so the next thing is to create a template object. Code <? Php $ t = new Template ( welcome to phpsu.com
"/ home / mydir / mytemplates /");?>
Create a new template object $ t. This $ t object is a handler, will be used to deal with all the template function, in other scripts for PHP code. If you wish, you may create a template to other objects (each one has its own template variable name space), but spent just one. Construction of the template function call in the path ( "/ home / mydir / mytemplates /") used to set the location of your template in the root directory, but not if you set it, it will default to your PHP script directory The same.
Then we call set_file () to define a group called "MyFileHandle" to handle the links with MyTemplate.ihtml (parse () is called before the template will not really load). Incidentally click, PHPLIB template of the template file name for the suffix. Ihtml is a habit, you can use. Html,. Tpl, or other suffixes. Then calls set_var () to set the template for $ my_color some_color variable the values (values are "blue"), means that all appear in the template (some_color) will be the local word "blue" by the replacement, once we call the parse (). Then we call parse (), it will be put into MyFileHandle (MyTemplate.ihtml) for analysis, and replace all the template variables ( "a variable ()") as a template variable values, analysis of the results on MyOutput. The results will not be any output to a web server, unless p ( "MyOutput") is called, it will export the final analysis of the text.
http://www.phpsu.com
Nested template
parse () function of the characteristics of a smart, it created MyOutput handler is a true template variables, as a some_color
A template variables. So if you have another template, it has a {MyOutput} labelling, when you analysis of the second template, all of the
{MyOutput} tag will be replaced by MyOutput in the analysis of the text. This feature allows you to embed a template file to another template
In. Therefore, we may have another called wholePage.ihtml templates, as follows:
I am sorry, you did not win. But if you win, you said that we will:
{MyOutput}
WholePage.ihtml and was in analysis, the final outcome will be:
I am sorry, you did not win. But if you win, you said that we will:
Congratulations! You won a new blue Honda Prelude!
Below are the two templates of the PHP code:
<? php
$ t = new Template ( "/ home / mydir / mytemplates /");
http://www.phpsu.com
/ / This first example with three lines of the same
$ t-> set_file ( "MyFileHandle", "MyTemplate.ihtml");
$ t-> set_var ( "some_color", $ my_color);
$ t-> parse ( "MyOutput", "MyFileHandle");
/ / We do not call attention to p ()
/ / Here, the output still not anything
/ / Now of the second template
$ t-> set_file ( "WholeHandle", "wholePage.ihtml");
/ / WholePage.ihtml have "(MyOutput)" inside
$ t-> parse ( "MyFinalOutput", "WholeHandle");
/ / (MyOutput) all be replaced
$ t-> p ( "MyFinalOutput");
/ / Output value MyFinalOutput
?>
Finally call the parse () and p () statements of two lines can be combined into one abbreviated function pparse ():
pparse ( "MyFinalOutput", "SecondHandle");
Another characteristic PHPLIB template is set_file () and set_var () function can also receive a multi-value, pass through a handler / array of arrays. This is an example: phpsu
<? php
$ t-> set_file (array ( "pageOneHandle" => "pageone.ihtml",
"pageTwoHandle" => "pagetwo.ihtml"));
$ t-> set_var (array ( "last_name" => "Gates",
"first_name" => "Bill",
"net_worth" => $ reallybignumber));
?>
Add template text
You can to parse () and pparse () The third transmission parameters, if you wish to give additional data template variables rather than replace it. Can be simple to use true as the third parameter called parse () and pparse (), such as: <? Php
$ t-> parse ( "MyOutput", "MyFileHandle", true);
?>
If MyOutput already contains data, MyFileHandle will be additional analysis and was to MyOutput existing data on. This technology is very useful, if you have a template, with a section of text you want to be repeated, for example, presents a database query results in multiple lines. You may also be displayed an array of variables, such as the following example: phpsu
<? php
$ t = new Template ( "/ home / mydir / mytemplates /"); $ t-> set_file (array ( "mainpage" => "mainpage.ihtml", "each_element" => "each_element.ihtml")); reset ($ myArray); while (list ($ elementName, $ elementValue) = each ($ myArray)) (
/ / Set 'value' and 'name' for each element and the value of name
$ t-> set_var ( "name", $ elementName); $ t-> set_var ( "value", $ elementValue);
/ / Copy of the additional each_element
$ t-> parse ( "array_elements", "each_element", true);) $ t-> pparse ( "output", "mainpage ");?>
This example has two template, mainpage.ihtml and each_element.ihtml. mainpage.ihtml template may be this:
Here is the array:
{array_elements}
Above {array_elements} each_element.ihtml labels will be replaced by a copy, it will in accordance with the array ($ myArray) repetition. each_element.ihtml template may seem:
phpsu提供的php教程
{name}: {value)}
The result is a deal with $ myArray contain elements of the format very good form. However, if the two merged into a template template is not better? » In fact, they can use the model to a combination of plate. Die plate from a template allowed to retrieve a text, so you can repeat many times, or do anything on it you want to do. But I will be in another article on this characteristic.
TITLE:Template, PHPLIB approach