P keyword: PHP4, template, template, IntegratedTemplateExtension, block reader requirement: it is very good to understand the template concept of PHP4 and program with PHP template class, but sometimes it will encounter a problem, for example, if a table is output, but the number of rows in the table is unknown only when running, such as message board, BBS, shopping website, and so on, this
P> Keywords: PHP4, template, template, IntegratedTemplateExtension, block
Reader requirements: Understand the PHP4 template concept
It is very good to use the PHP template class for programming, but sometimes you may encounter a problem, such as outputting a table, but the number of rows of the table is not known until it is run, such as message boards, BBS, and shopping websites. At this time, the artist cannot decide to use several rows of tables in the HTML file. if loop output is written in the PHP code file, it will make it inconvenient for the artist and PHP programmers to view the code, the artist will say, where is the form? What should I do if I want to modify the color and background of the table? PHP programmers will also say, why is there,, What is the purpose? Where is the HTML file embedded ?.
PHP template-type programming generally treats this uncertain number of HTML elements as a "block". bolck programming is similar to writing a loop in code. This function is available in common PHP template classes (such as FastTemplate and PHPLib. Writing nested blocks is similar to writing multiple loops. Now we will give an example to illustrate the block programming method in the IntegratedTemplateExtension class in PHP4. In this example, we will use a two-repeating loop, the outer block is GoodsList, and the layer block is GoodsListOfSomeType.
Basic settings: assume that the code we wrote is stored in C: \ TestPHP \ PHP4 \ GoodsList.htm and C: \ TestPHP \ HTML \ GoodsList. php. Set C: \ TestPHP \ PHP4 to a virtual directory/testphp on the Web Server and grant the script execution permission. make sure that C: \ TestPHP \ HTML \ GoodsList.htm cannot be accessed through a remote browser. If PHP4 is installed in C: \ php4, set include_path = ".; C: \ php4 \ pear" in php. ini"
The following is the goodslist.htm content:
Commodity list in shopping bag
|
{UserName}: your shopping bag contains the following items:
|
|
|
Product Type |
Product Name |
Product Price |
|
|
{Type} |
|
|
|
|
|
{GoodsName} |
{Price} |
|
|
The following is the PHP4 code file GoodsList. php.
Require_once "HTML/ITX. php ";
File: // assign values to variables. in actual code, you may obtain data from the Database and assign values.
$ UserName = "pipiru ";
$ GoodsTypeArray = array ("household appliances", "books ");
$ GoodsNameArray = array ("Samsung Display", "Sony single-host", "Changhong TV "),
Array ("C ++ programming ideology", "Java 2 Advanced Development Guide", "Visual Basic 5 Advanced Development Guide ",
"Flash 4 Flash pages", "design patterns can reuse the basis of Object-Oriented Software "));
$ GoodsPriceArray = array (1024,302,102 4 ),
Array (35,62, 76, 66.5, 55 ));
File: // generally, this global variable is placed in a separate file for easy maintenance.
$ HTML_CODE_FILE_ROOT = "../HTML /";
$ Tpl = new IntegratedTemplateExtension ($ HTML_CODE_FILE_ROOT );
File: // specifies the HTML file to replace the tag
$ Tpl-> loadTemplatefile ("GoodsList.htm ");
$ Tpl-> setVariable ("UserName", $ UserName); file: // User name
File: // specify the name of the outer block
$ Tpl-> setCurrentBlock ("GoodsList ");
File: // I like to assign the number of cycles separately before the loop.
$ GoodsTypeCount = count ($ GoodsTypeArray );
File: // loop the external layer block
For ($ I = 0; $ I <$ GoodsTypeCount; $ I ++)
{
$ Tpl-> setVariable ("Type", $ GoodsTypeArray [$ I]); file: // goods Type
File: // specify the block name of the layer.
$ Tpl-> setCurrentBlock ("GoodsListOfSomeType ");
$ GoodsNameArrayCount = count ($ GoodsNameArray [$ I]);
File: // loop the layer-1 block
For ($ j = 0; $ j <$ GoodsNameArrayCount; $ j ++)
{
File: // replace the tag in the HTML file
$ Tpl-> setVariable (array ("GoodsName" => $ GoodsNameArray [$ I] [$ j],
"Price" => $ GoodsPriceArray [$ I] [$ j]);
$ Tpl-> parsecurityblock (); file: // Here you can also write $ tpl-> parse ("GoodsListOfSomeType ");
}
$ Tpl-> parse ("GoodsList"); file: // ends the outer block.
}
File: // output the replaced HTML
$ Tpl-> show ();
?>
After running the command, you can see the effect of multiple loop replacement. After writing the code in this way, GoodsList.htm can still be edited using webpage editors such as Dreamweaver and FrontPage. GoodsList. php contains pure PHP code without HTML, which facilitates code modification and maintenance in the future.