欢迎光临
我们一直在努力

PEAR里HTML_Template_IT摸板类的用法-PHP教程,PHP应用

建站超值云服务器,限时71元/月

pear里的html_template_it是一个简单而且容易使用的类。

********** 建立一个模板文件 ****************

“符号”命名规则 为下面的正则表达式:

{[0-9a-za-z_-]+}

”块“ 格式为下面的,块名的命名规则和“符号”一样的:

<!– begin 块名 –>

… 块内容 …

<!– end 块名 –>

块允许嵌套,你必须首先设置并分析最内层的块,然后再设置和分析上一层的块。

*********** 主要方法 ***********

构造函数:

void integratedtemplate::integratedtemplate ([string $root = ""])

$root = 摸板根目录

你也可以用

void integratedtemplate::setroot (string $root)

方法设置模板目录;该方法不能被静态调用。

———————————————————

加载模板:

boolean integratedtemplate::loadtemplatefile (string $filename, boolean [$removeunknownvariables = true], boolean [$removeemptyblocks = true])

$filename = 模板文件名

$removeunknownvariables = 设置是否移除未知的变量

$removeemptyblocks = 设置是否移除空块

成功返回true,失败返回false。

该方法不能被静态调用

——————————————————-

指定当前要处理的块:

boolean integratedtemplate::setcurrentblock ([string $block = "__global"])

$block = 块名

成功返回true,失败会返回一个pear_error对象,里面包含错误

描述。该方法不能被静态调用。

——————————————————-

用变量替换当前要处理的块里的“符号”:

void integratedtemplate::setvariable (mixed $placeholder [, mixed $variable = ""])

$placeholder = 要替换的符号名,如果$variable是一个数组,

那就用符号名作为这个数组的索引并用这个索

引的数组元素的数据替换符号

$variable = 变量名或数组名

该函数不能被静态调用

——————————————————–

分析当前正处理的块:

void integratedtemplate::parsecurrentblock ()

失败会返回一个pear_error对象,里面包含错误

描述。该方法不能被静态调用。

———————————————————

处理完成输出:

void integratedtemplate::show ([string $block])

$block = 要返回的块

如果没设置$block,将返回完整的模板

******************* 简单示例(示例来自pear的在线手册)*****

usage example

example 25-1. main.tpl.htm模板文件,在"./templates"目录下

<html>

<table border>

<!– begin row –>

<tr>

<!– begin cell –>

<td>

{data}

</td>

<!– end cell –>

</tr>

<!– end row –>

</table>

</html>

example 25-2. php脚本

<?php

require_once "html/template/it.php";

$data = array

(

"0" => array("stig", "bakken"),

"1" => array("martin", "jansen"),

"2" => array("alexander", "merz")

);

$tpl = new html_template_it("./templates");

$tpl->loadtemplatefile("main.tpl.htm", true, true);

foreach($data as $name) {

foreach($name as $cell) {

// assign data to the inner block

$tpl->setcurrentblock("cell") ;

$tpl->setvariable("data", $cell) ;

$tpl->parsecurrentblock("cell") ;

}

// assign data and the inner block to the

// outer block

$tpl->setcurrentblock("row") ;

$tpl->parsecurrentblock("row") ;

}

// 输出

$tpl->show();

?>

example 25-3. 输出

<html>

<table border>

<tr>

<td>

stig

</td>

<td>

bakken

</td>

</tr>

<tr>

<td>

martin

</td>

<td>

jansen

</td>

</tr>

<tr>

<td>

alexander

</td>

<td>

merz

</td>

</tr>

</table>

</html>

*******************************************************

html_template_it里面还有一个itx类,可以完成更加复杂的功能,

继承自it类;itx。php文件的原代码里包含了这个类的方法说明。

关于html_template_it其他方法的详细说明请参见pear的手册,地址为

http://pear.php.net/manual/en/package.html.html-template-it.php

–希望大家能好好利用pear这个库写出更多更好的php程序!–

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » PEAR里HTML_Template_IT摸板类的用法-PHP教程,PHP应用
分享到: 更多 (0)

相关推荐

  • 暂无文章