If you want to know what Templates are, first take a look at the wonderful article "Templates-why and how to use them in PHP3" (Templates-why and how to use them in PHP3) written by Sascha Schumann) the first few paragraphs. In general, templates allow you to completely separate your PHP code from HTML, which makes HTML graphic designers very
Be happy and avoid losing your valuable designs.
It is not FastTemplates
So, do we really need another article about the template on PHPBuilder? Well, yes, because there are more than one way to use PHP
To implement the template. Sascha's article describes how to use FastTEmplates, but the PHP Basic Library ("PHPLIB") has its own template
Now.
What are their differences? FastTemplates was originally transformed from a Perl library. FastTemplates is very useful for Perl programs.
Yes, but it is not ideal for PHP. Kristian Koehntopp has compiled the PHPLIB template from scratch. As a pure PHP library, it provides better
The advantage of PHP. One of the advantages is that Kristian's design uses preg_replace () to analyze the template.
The ereg_replace () of is faster. Another benefit of the PHPLIB template is that it allows dynamic block implementations to be nested, unlike FastTemplates.
Both databases have very similar features and capabilities, but if you have already used FastTemplates and want to learn how to use PHPLIB
Template, you should forget everything you know about FastTemplates. Their features may be similar, but the PHPLIB Template
Everything you do is a little different than FastTemplates.
Use PHPLIB Template
Let's start with a simple example. Assume that there is a module named MyTemplate under/home/mydir/mytemplates/.
Board, which has some text, the content may be:
Congratulations! You won a {some_color} Honda Prelude!
Note that "{some_color}" is surrounded by braces. Braces indicate that some_color is a template variable. We may want to write
Script, it can load the template, insert the PHP variable $ my_color value in the {some_color} template variable, and then output new text.
If $ my_color happens to be set to "blue", the final output may be:
Congratulations! You win a new blue Honda Prelude!
The following is the PHP script for the above results:
--------------------------------------------------------------------------------
<? Php Tutorial
Include "template. inc ";
$ My_color = "blue ";
// Will be used later
$ T = new Template ("/home/mydir/mytemplates /");
// Create a template object named $ t
$ T-> set_file ("MyFileHandle", "MyTemplate. ihtml ");
// Set MyFileHandle = our template file
$ T-> set_var ("some_color", $ my_color );
// Set the template variable some_color = $ my_color.
$ T-> parse ("MyOutput", "MyFileHandle ");
// Set the template variable MyOutput = the analyzed file
$ T-> p ("MyOutput ");
// Output the value of MyOutput (the data after analysis)
?> --------------------------------------------------------------------------------
The first line is an include Command to provide the PHPLIB template function. Of course, PHPLIB does more than the template, but if you only want
To use the template feature, you only need to include tmplate. inc (template. inc is one of the files from PHPLIB ). PHPLIB Template User interface
Design to the object program, so the next thing is to create a template object. Code <? $ T = new Template (
"/Home/mydir/mytemplates/");?> Create a new template object $ t. This $ t object is a handle that will be used to process
All template functions are used for other code in the PHP script. If you want to, you may create other template objects (each with its own
Template variable namespace), but one is enough. Path in the template's constructor call ("/home/mydir/mytemplates /")
It is used to set the root directory where your template is located, but if you do not set it, it will be the same as the directory where your PHP script is located by default.
Then we call set_file () to define a handle named "MyFileHandle" and link it to MyTemplate. ihtml (in parse ()
Before being called, the template will not actually be loaded ). By the way, the extension of the template file name of the PHPLIB template is. ihtml,
You can use .html,. tpl, or other suffixes. Call set_var () to set the value of the template variable some_color to $ my_color (
The value is "blue"), meaning that all the places where {some_color} appears in the template will be replaced by the word "blue". Once we call
Parse ().
Next we call parse (), which loads MyFileHandle (MyTemplate. ihtml) for analysis and replaces all template variables ("
{A variable} ") is the value of the template variable, and the analysis result is placed in MyOutput. No results will be output to the web server unless
P ("MyOutput") is called to output the last analyzed text.