当前位置: 首页 > 图文教程 > 网络编程 > PHP > IntegratedTemplate类实现BLOCK功能

PHP
MYSQL版本大于4.1问题 - PHPchina
怎么让用户点击一个连接后,把一个图片另存了 - PHPchina
武汉10月15日Phper聚会召集!!! - PHPchina
php如果不等待exec执行的程序创建的子进程? - PHPchina
哪位知道DISCUZ处理防SQL注入的代码是哪部分 - PHPchina
求教!我实在不知道哪里问题,在线等ing - PHPchina
怎样结束用户某一进程 - PHPchina
比对用户名密码能不能这样写? - PHPchina
求助:如何在PHP+mysql中实现数据备份? - PHPchina
大家看看这个配置对吗 - PHPchina
如何禁止require当前文件 - PHPchina
无法将回调函数放在类中? - PHPchina
村里 PHP代码高亮是怎么实现的? - PHPchina
apache安装后.服务里没有apache2这个服务! - PHPchina
请教一个小问题 - PHPchina
config.php里面是不是应该把多数参数设置为常量而不是变量? - PHPchina
请教高手一个问题 - PHPchina
如何让百度收录我的网站 ?? - PHPchina
谁能给个注入的简单语句? - PHPchina
求PHP站内搜索思路 - PHPchina

PHP 中的 IntegratedTemplate类实现BLOCK功能


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 196 ::
收藏到网摘: n/a

读者要求:了解PHP4 的模板概念
使用PHP 模板类进行编程很有好处,但是有时也会碰到一个问题,比如说输出一个表格,但是表格行数要到运行的时候才知道,如留言板、BBS、购物网站之类,经常会碰到这个问题。这时做美工的人无法决定在HTML文件中用几行表格,如果在PHP代码文件中写循环输出,又会让美工、PHP程序员看代码都不方便,美工的人会说,这里的表格哪里去了?我要修改表格的颜色背景之类怎么办?PHP程序员也会说,怎么这里突然有一个<tr>、<td>,做什么用?会嵌在HTML文件哪里?。
使用PHP模板类编程一般把这种不确定个数的HTML 元素当成一个“ block ”,对 bolck 的编程类似于在代码中写一个循环。在比较常用的PHP模板类(如 FastTemplate 和 PHPLib )都有这种功能。写嵌套的block 类似于写多重循环。现在举例说明在 PHP4 里面的 IntegratedTemplateExtension 类中block 的编程方法,例子中用的是两重循环,外层block 是GoodsList,里层block 是GoodsListOfSomeType 。
基本设置:假设我们写的代码放在C:\TestPHP\PHP4\GoodsList.htm 和 C:\TestPHP\HTML\GoodsList.php 中。将C:\TestPHP\PHP4 在Web Server 上设成虚拟目录 /testphp 并且给与脚本执行权限,确认C:\TestPHP\HTML\GoodsList.htm 无法通过远端浏览器访问。假定PHP4安装在C:\php4,在 php.ini 里面设置 include_path = ".;C:\php4\pear"

以下是GoodsList.htm的内容:


<html>
<head>
<title>购物袋里的商品清单</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},您的购物袋里有以下商品:</div>
</td>
</tr>
<tr>
<td colspan="5">
<hr>
</td>
</tr>
<tr>
<td width="52">&nbsp;</td>
<td width="302">商品种类</td>
<td width="302">商品名称</td>
<td width="299">商品价格</td>
<td width="47">&nbsp;</td>
</tr>
<!-- BEGIN GoodsList --> <!-- 说明: block 商品列表开始 -->
<tr bgcolor="#99CCFF">
<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 --> <!-- 说明: block 某一类商品列表开始 -->
<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 --> <!-- 说明: block 某一类商品列表结束 -->
<!-- END GoodsList --> <!-- 说明: block 商品列表结束 -->
<tr>
<td colspan="5">&nbsp;</td>
</tr>
</table>
<p>&nbsp;</p>
</body>
</html>

 

以下是PHP4代码文件 GoodsList.php


<?php
require_once "HTML/ITX.php";
// 以下是给变量赋值,在实际代码中可能从Database中取得数据然后赋值
$UserName = "皮皮鲁";
$GoodsTypeArray = array("家电", "书籍");
$GoodsNameArray = array(array("三星显示器", "Sony单放机&quo