Summary:
When many people develop large PHP projects, the template technology is very useful. It can separate the work of artists and programmers and facilitate the modification and improvement of the interface, we can also easily and effectively customize or modify websites. Now we will use the PHPLIB template as an example to describe how to apply the template technology in PHP.
How to use the PHPLIB template?
We have a template named UserTemp with the path/home/user_dir/user_temp/. Its content is as follows:
What you ordered: {Product}
Braces indicate that Product is a template variable.
Then we write the following program:
<? Php
Include "template. inc ";
$ User_product = "Walkman ";
$ Tmp = new Template ("/home/user_dir/user_temp/"); // Create a Template object named $ t
$ Tmp-> set_file ("FileHandle", "UserTemp. ihtml"); // Set the handle FileHandle = template file
$ Tmp-> set_var ("Product", $ user_product); // set the template variable Product = $ user_product
$ Tmp-> parse ("Output", "FileHandle"); // set the template variable Output = file after Analysis
$ Tmp-> p ("Output"); // Output the Output value (the data after analysis)
?>
Template. inc is a file in PHPLIB. We use include to use the PHPLIB template function. The PHPLIB Template uses an object-oriented design, so we can use $ tmp = new Template ("/home/user_dir/user_temp/") to create a Template object, the parameter is a path ("/home/user_dir/user_temp/") used to set the location of the template file. The default path is the directory where the PHP script is located.
Set_file () is used to define the pointer to UserTemp. ihtml (the extension of the template file name of the PHPLIB template is. ihtml) handle "FileHandle", set_var () is used to set the template variable "Product" to $ user_product value (that is, "Walkman"), the parse () method will load FileHandle (that is, UserTemp. ihtml) for analysis, replace all "{Product}" that appears in the template with the value of $ user_product ("Walkman ").
How to use nested templates?
In the preceding example, the "Output" set by the parse () method is a template variable. With this, we can implement template nesting.
For example, we have another template (assume UserTemp2) with the following content:
Welcome, dear friend! {Output}
After analysis, the output will be:
Welcome, dear friend! You ordered: Walkman
The updated program is as follows:
<? Php
Include "template. inc ";
$ User_product = "Walkman ";
$ Tmp = new Template ("/home/user_dir/user_temp /");
$ Tmp-> set_file ("FileHandle", "UserTemp. ihtml ");
$ Tmp-> set_var ("Product", $ user_product );
$ Tmp-> parse ("Output", "FileHandle ");
$ Tmp-> set_file ("FileHandle2", "UserTemp2.ihtml"); // you can specify the second template handle.
$ Tmp-> parse ("Output", "FileHandle2"); // analyze the second template
$ Tmp-> p ("Output ");
?>
It is very simple. We will not explain it in detail. Here is a tip: parse () and p () can be written as a function pparse (), such as $ tmp-> pparse (Output "," FileHandle2 ).
How does the PHPLIB template accept multiple sets of values?
The parameters of setfile () and set_var () can be associated arrays (handles are used as array indexes and template files as values), so that the template can accept multiple values.