Using the Integratedtemplate class in PHP4 to implement the Block function

Source: Internet
Author: User
Tags array count implement php code php template variable
Using PHP template classes for programming is good, but sometimes there will be a problem, such as output a table, but the number of table rows to run when you know, such as message board, BBS, shopping websites and so on, often encounter this problem. At this time to do art people can not decide to use a few lines in the HTML file table, if the PHP code file in the write cycle output, but also to the art, PHP programmers look at the code is inconvenient, art people will say, here's the table where? What if I want to change the color background of the form? PHP programmers will also say, how here suddenly has a <tr>, <td> Where is the HTML file embedded?
Using PHP template class programming generally treats this indeterminate number of HTML elements as a "block", the programming of BOLCK is similar to writing a loop in code. This functionality is available in the more commonly used PHP template classes, such as Fasttemplate and Phplib. Writing nested blocks is similar to writing multiple loops. Now for example, in the PHP4 inside the Integratedtemplateextension class block's programming method, the example uses is the double loop, the outer block is the goodslist, the innermost block is the goodslistofsometype.
Basic settings: Suppose we write code in C:\TestPHP\PHP4\GoodsList.htm and C:\TestPHP\HTML\GoodsList.php. Make C:\TestPHP\PHP4 a virtual directory on the Web Server/testphp and Execute permissions on the script, confirming that C:\TestPHP\HTML\GoodsList.htm cannot be accessed through a remote browser. Suppose the PHP4 is installed in the C:\PHP4, and the php.ini is set include_path = ".; C:\php4\pear "

Here's what goodslist.htm:


<title> shopping bags in the list of goods </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>

<body bgcolor= "#FFFFFF" text= "#000000" >
<table width= "border=" "0" cellspacing= "0" cellpadding= "0" height= ">"
<tr>
&LT;TD colspan= "5" > </td>
</tr>
<tr>
&LT;TD colspan= "5" >
<div align= "Center" >{username}, you have the following items in your shopping bag:</div>
</td>
</tr>
<tr>
&LT;TD colspan= "5" >
</td>
</tr>
<tr>
&LT;TD width= > </td>
&LT;TD width= "302" > Product category </td>
&LT;TD width= "302" > Product name </td>
&LT;TD width= "299" > Commodity price </td>
&LT;TD width= "> </td>"
</tr>
<!--begin Goodslist--> <!--description: Block Items list starts-->
<tr bgcolor= "#99CCFF" >
&LT;TD width= > </td>
&LT;TD width= "302" ><font color= "#CC0066" >{Type}</font></td>
&LT;TD width= "302" > </td>
&LT;TD width= "299" > </td>
&LT;TD width= "> </td>"
</tr>
<!--begin Goodslistofsometype--> <!--Description: Block a category of goods List began-->
<tr>
&LT;TD width= > </td>
&LT;TD width= "302" > </td>
&LT;TD width= "302" >{GoodsName}</td>
&LT;TD width= "299" >{Price}</td>
&LT;TD width= "> </td>"
</tr>
<!--end Goodslistofsometype--> <!--Description: Block a category of goods List ends-->
<!--end Goodslist--> <!--Description: Block Item List ends-->
<tr>
&LT;TD colspan= "5" > </td>
</tr>
</table>
<p> </p>
</body>


The following is the PHP4 code file goodslist.php


<?php
Require_once "html/itx.php";
Here's the assignment to the variable, where you can get the data from the database in the actual code and then assign the value
$UserName = "Pi Lu";
$GoodsTypeArray = Array ("Home appliances", "books");
$GoodsNameArray = Array ("Samsung Display", "Sony Multiplayer", "Changhong color TV"),
Array ("C + + programming Idea", "Java 2 Advanced Development Guide", "Visual Basic 5 Advanced Development Guide",
Flash 4 Flash Web page, "design pattern reusable object-oriented software");
$GoodsPriceArray = Array (Array (1024, 302, 1024),
Array (35, 62, 76, 66.5, 55));

In general, this global variable is placed in a separate file for easy maintenance
$HTML _code_file_root = ". /html/";

$TPL = new Integratedtemplateextension ($HTML _code_file_root);
Specify the HTML file to replace tag
$tpl->loadtemplatefile ("goodslist.htm");

$tpl->setvariable ("UserName", $UserName); User name

Specify the outer block name
$tpl->setcurrentblock ("goodslist");
I like to assign the number of loops to a single value before the loop.


$GoodsTypeCount = count ($GoodsTypeArray);

Loop on the outer layer block
for ($i = 0; $i < $GoodsTypeCount; $i + +)
{
$tpl->setvariable ("Type", $GoodsTypeArray [$i]); Types of goods

Specify the inner block name
$tpl->setcurrentblock ("Goodslistofsometype");

$GoodsNameArrayCount = count ($GoodsNameArray [$i]);

Loop through the inner block
for ($j = 0; $j < $GoodsNameArrayCount; $j + +)
{
Replace the tag in an HTML file
$TPL->setvariable (Array ("Goodsname" => $GoodsNameArray [$i] [$j],
"Price" => $GoodsPriceArray [$i] [$j]);
$tpl->parsecurrentblock (); Here also can write $tpl->parse ("Goodslistofsometype");
}
$tpl->parse ("goodslist"); END Outer block
}

Output after the replacement of HTML
$tpl->show ();
?>




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.