Home  >  Article  >  Backend Development  >  PHP中的模板技术_PHP

PHP中的模板技术_PHP

WBOY
WBOYOriginal
2016-06-01 12:34:40951browse
综述:

在多人开发大型PHP项目时,模板技术非常有用,它可以分开美工和程序员的工作,并且方便界面的修改和完善;不仅如此,利用模板技术,我们还可以简单有效地定制或者修改站点。现在我们将要以PHPLIB的模板为例子讲述如何在PHP中应用模板技术。

  如何使用PHPLIB模板?

设我们有一个模板, 名为UserTemp,路径为/home/user_dir/user_temp/,它的内容如下:

你订购的是:{Product}

大括号表示Product是一个模板变量。

然后我们编写如下的程序:

<?php
include "template.inc";
$user_product = "随身听";

$tmp = new Template("/home/user_dir/user_temp/"); // 创建一个名为 $t 的模板对象
$tmp->set_file("FileHandle","UserTemp.ihtml"); // 设置句柄FileHandle = 模板文件
$tmp->set_var("Product",$user_product); // 设置模板变量Product=$user_product
$tmp->parse("Output","FileHandle"); // 设置模板变量 Output = 分析后的文件
$tmp->p("Output"); // 输出 Output 的值(我们的分析后的数据)
?>

template.inc是PHPLIB中的一个文件,我们用include以便使用PHPLIB的模板功能。PHPLIB模板使用的是面向对象的设计,所以我们可以用$tmp = new Template("/home/user_dir/user_temp/")创建一个模板对象,其参数是一个路径("/home/user_dir/user_temp/"), 用来设置模板文件所在位置,默认路径是PHP脚本所在目录。

set_file()用来定义指向UserTemp.ihtml(PHPLIB模板的模板文件名的后缀为.ihtml )的句柄"FileHandle",set_var()用来设置模板变量Product为$user_product的值(即"随身听"),parse()方法会装入FileHandle(即UserTemp.ihtml)进行分析,将所有在模板中出现的"{Product}"替换成$user_product的值("随身听")。

  如何使用嵌套的模板?

在上面的例子中,parse()方法设置的"Output"是一个模板变量,利用这点,我们可以实现模板的嵌套。

比如,我们有另外一个模板(假设为UserTemp2),其内容是:

欢迎你,亲爱的朋友!{Output}

那么在分析之后,其输出会是:

欢迎你,亲爱的朋友!你订购的是:随身听

下面是更新后的程序:

<?php
include "template.inc";
$user_product = "随身听";
$tmp = new Template("/home/user_dir/user_temp/");
$tmp->set_file("FileHandle","UserTemp.ihtml");
$tmp->set_var("Product",$user_product);
$tmp->parse("Output","FileHandle");

$tmp->set_file("FileHandle2","UserTemp2.ihtml");//设置第二个模板句柄
$tmp->parse("Output","FileHandle2");//分析第二个模板
$tmp->p("Output");
?>


很简单,我们就不详细解释了。这里有一个技巧:parse()和p()可以写成一个函数pparse(),比如$tmp->pparse(Output","FileHandle2)。

  PHPLIB模板如何接受多组值?

setfile()和set_var()的参数可以是关联数组(句柄作为数组索引,模板文件作为值),这样模板就可以接受多个值,比如:

<?php
……
$tmp->setfile(array("FileHandle"=>"UserTemp.ihtml","FileHandle2"=>"UserTemp2.ihtml"));
$tmp->set_var(array("Product"=>"随身听","Product2"=>"电视机"));
……
?>

  如何给模板变量追加数据?

我们可以给parse()和pparse()提供第三个参数(布尔变量)来给模板变量追加数据:

<?php
……
$tmp->pparse("Output","FileHandle",true);
……
?>

这样,FileHandle被分析后就会被追加到Output变量的值的后面而不是简单的替换。

  为什么要使用block机制?

  比方说我们想要显示:

你订购的是:随身听 电视机,……

用上面的方法直接追加的话,可能显示出来的是:

你订购的是:随身听 你订购的是:电视机 你订购的是:……

显然不符合我们的要求,那么如何有效解决这个问题呢?这里就要使用block机制。

我们将上面的模板文件UserTemp.ihtml修改一下:

你订购的是:

<!-- BEGIN Product_List -->
{Product}
<!-- END Product_List -->

  这样我们就定义了一个名为"Product_List"的block。

  相应的程序为:

<?php
include "template.inc";
$tmp=new Template("/home/user_dir/user_temp/");
$tmp->set_file("FileHandle","UserTemp.ihtml");
$tmp->set_block("FileHandle","Product_List","Product_Lists");
//将文件中的block替换成{Product_Lists}

$tmp->set_var("Product","随身听");
$tmp->parse("Product_Lists","Product_List",true);
$tmp->set_var("Product","电视机");
$tmp->parse("Product_Lists","Product_List",true);
//具体使用中,可以用数组和循环来做

$tmp->parse("Output","FileHandle");
$tmp->p("Output");
?>

  现在的输出就是我们想要的结果了。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn