代码分离
使用php编程的人都会碰到这样一个问题:当php代码和html代码在一起的时候,看php代码很费劲,整个文件也无法用dreamweaver来编辑,这对于php程序员和美工来讲,修改这样的文件就象一个噩梦。
php中的模板(template)技术就是为了解决这个问题而出现的。php模板类有很多,比较常见的是 fasttemplate 和 phplib, 因为出现得早,在php编程界名声很大。php程序员不知道这两个类,就象vb程序员不知道msgbox函数一样,是一件不可思议的事情。
以前我们需要去下载php模板类,现在php4有了自己的模板类 integratedtemplate 和 integratedtemplateextension,功能和phplib 差不多。这两个类是子类和父类的关系。一般我们用 integratedtemplateextension 就可以了。让人感到不可思议的是 integratedtemplate 不是从 pear 类继承过来,无法用到 pear 类的 debug 功能。
以下举例子说明它们的用法,假设 integratedtemplate 类和 integratedtemplateextension 类分别在 c:\php4\pear\html\itx.php 和 c:\php4\pear\html\itx.php 中。我们写的代码放在c:\testphp\php4\welcome.htm 和 c:\testphp\html\welcome.php 中。将c:\testphp\php4 在web server 上设成虚拟目录 /testphp 并且给与脚本执行权限,确认c:\testphp\html\welcome.htm 无法通过远端浏览器访问。在 php.ini 里面设置 include_path = .;c:\php4\pear
例1:
我们在html文件中放置变量的标记,用php代码设置变量的值,然后将html中的标记替换掉,最后输出到客户浏览器。
以下是 welcome.htm 代码,我们放了三个php tag(变量标记)为: {welcometitle}、{username}、{welcomemessage}
{welcometitle}