Use the integratedtemplate class in PhP4 to implement the Block Function

Source: Internet
Author: User

Introduction: This is a detailed page for implementing the block function using the integratedtemplate class in PhP4. It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 324605 'rolling = 'no'>

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.CodeWrite loop output in the file, it will make the artist, PHPProgramIt is inconvenient for the engineer 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 a sudden <tr> and <TD>? 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:
<HTML>
<Head>
<Title> commodity list in shopping bags </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Style type = "text/CSS">
<! --
Body, P, BR, TD, TR, table {font-size: 9pt}
-->
</Style>
</Head>

<Body bgcolor = "# ffffff" text = "#000000">
<Table width = "700" border = "0" cellspacing = "0" cellpadding = "0" Height = "90">
<Tr>
<TD colspan = "5"> & nbsp; </TD>
</Tr>
<Tr>
<TD colspan = "5">
<Div align = "center"> {username}: Your shopping bag contains the following items: </div>
</TD>
</Tr>
<Tr>
<TD colspan = "5">
<HR>
</TD>
</Tr>
<Tr>
<TD width = "52"> & nbsp; </TD>
<TD width = "302"> item type </TD>
<TD width = "302"> product name </TD>
<TD width = "299"> product price </TD>
<TD width = "47"> & nbsp; </TD>
</Tr>
<! -- Begin goodslist --> <! -- Note: The block product list starts -->
<Tr bgcolor = "#99 CCFF">
<TD width = "52"> & nbsp; </TD>
<TD width = "302"> <font color = "# cc0066"> {type} </font> </TD>
<TD width = "302"> & nbsp; </TD>
<TD width = "299"> & nbsp; </TD>
<TD width = "47"> & nbsp; </TD>
</Tr>
<! -- Begin goodslistofsometype --> <! -- Note: block the beginning of a category of commodity list -->
<Tr>
<TD width = "52"> & nbsp; </TD>
<TD width = "302"> & nbsp; </TD>
<TD width = "302" >{ goodsname} </TD>
<TD width = "299" >{ price} </TD>
<TD width = "47"> & nbsp; </TD>
</Tr>
<! -- End goodslistofsometype --> <! -- Description: The end of a block type commodity list -->
<! -- End goodslist --> <! -- Note: The block product list ends -->
<Tr>
<TD colspan = "5"> & nbsp; </TD>
</Tr>
</Table>
<P> & nbsp; </P>
</Body>
</Html>

The following is the PhP4 code file goodslist. php
require_once "html/itx. PHP ";
file: // assign a value to the variable, in actual code, data may be obtained from the database and then assigned a value
$ username = "pipiru";
$ goodstypearray = array ("appliances", "books ");
$ goodsnamearray = array (Array ("Samsung display", "Sony single-host", "Changhong color TV"),
array ("C ++ programming ideas ", "Java 2 Advanced Development Guide", "Visual Basic 5 Advanced Development Guide",
"Flash 4 burst web page ", "Design Pattern reusable basics of object-oriented software");
$ goodspricearray = array (Array (1024,302,102 4),
array (35, 62, 76, 66.5, 55);
file: // generally, this type of global variable is stored in a separate file for easy maintenance
$ html_code_file_root = ".. /html/";
$ TPL = new integratedtemplateextension ($ html_code_file_root);
file: // specify 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.

More articles on "using the integratedtemplate class in PhP4 to implement the block function"

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/324605.html pageno: 14

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.