Summary :?? When many people develop large PHP projects, the template technique is very useful. it can leave the work of artists and programmers and facilitate the modification and improvement of the interface. In addition, the template technique is used, we can also simply and effectively customize or repair
Summary:
?? When many people develop large PHP projects, the template technique is very useful. it can leave the work of artists and programmers and facilitate the modification and improvement of the interface. In addition, the template technique is used, we can also easily and effectively customize or modify sites. Now we will use the PHPLIB template as an example to describe how to use the template technique in PHP.
How to apply 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/'); // creates 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'); // you can specify 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 apply the template function of PHPLIB. The PHPLIB Template applies the 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 be loaded into FileHandle (that is, UserTemp. ihtml) to convert all '{Product}' displayed in the template to $ user_product ('walkman ').
How to apply 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 and we will not detail it. Here is a tip: parse () and p () can be written as a function pparse (), such as $ tmp-> pparse (output', 'filehandle2 ).