收 藏 投 稿 繁 体 RSS 
站长吧-中国站长学习与交流的网站
首 页 运 营 学 院 建 站 论 坛
Web master8.net  
 网站运营  
  欢迎在本站发布信息,在线投递稿件请点这里。编辑QQ:4908220,欢迎联系交流。
业界动态 创业故事 推广研究 策划盈利 电子商务 企业平台
  站长工具
SEO查询 Whois查询 Pr查询 域名查询 IP查询 网页编辑器
 建站服务  
  如有建站意向,请尽快联系我们,以便安排时间... 建站服务 QQ4908220 QQ:4908220
作品展示 服务范围 服务流程 服务报价 联系方式 付款方式
文章正文  » 您的当前位置: 首页 >> 学院 >> 程序开发 >> PHP
一个易用的模板类
  来源:互联网 | 时间:2006-03-08 | 浏览:   相关评论 | 报告错误 | 发布文章
【字号: | | 】 【背景色 杏仁黄 秋叶褐 胭脂红 芥末绿 天蓝 雪青 灰 银河白(默认色)
ONT>preg_quote("{".$varname."}");
}

/* private: loadfile(string $handle)
* handle: load file defined by handle, if it is not loaded yet.
*/
function loadfile($handle) {
if (isset(
$this->varkeys[$handle]) and !empty($this->varvals[$handle]))
return
true;

if (!isset(
$this->file[$handle])) {
$this->halt("loadfile: $handle is not a valid handle.");
return
false;
}
$filename = $this->file[$handle];

$str = implode("", @file($filename));
if (empty(
$str)) {
$this->halt("loadfile: While loading $handle, $filename does not exist or is empty.");
return
false;
}

$this->set_var($handle, $str);

return
true;
}

/***************************************************************************/
/* public: halt(string $msg)
* msg: error message to show.
*/
function halt($msg) {
$this->last_error = $msg;

if (
$this->halt_on_error != "no")
$this->haltmsg($msg);

if (
$this->halt_on_error == "yes")
die(
"<b>Halted.</b>");

return
false;
}

/* public, override: haltmsg($msg)
* msg: error message to show.
*/
function haltmsg($msg) {
printf("<b>Template Error:</b> %s<br>n", $msg);
}
}
#end Template_base class

//类名:Template
//功能:模板处理扩展
//说明:继承于Tempalte_base,修改了部分的模板处理函数
class Template extends Template_base {
var
$handelkey = array();
var
$handelcount;

function
Template($filename) {
$this->Template_base();
if(empty(
$filename) || !file_exists($filename)) {
die(
"Template -> Template() : Error - file $filename does not exist");
}
$this->set_file('ihtml',$filename);
$this->handelcount = 1;
return
true;
}

function
Output() {
$this->p('out');
return
true;
}

function
Compile() {
$this->parse('out','ihtml');
return
true;
}

function
OP() {
$copyright = '<p align=center>&copy; 2002 new Maya workroom</p>';
$this->SetVar('copyright',$copyright);
$this->Compile();
$this->OutPut();
return
true;
}

//example: var or array
// $key = array(
// 'row1' => '序号',
// 'row2' => '姓名',
// 'row3' => '性别'
// )
function SetVar($key,$value = '') {
$this->set_var($key,$value);
return
true;
}

// $key is define the current block
function SetBlock($blockname) {
$this->handelkey[$blockname] = $this->handelcount;
$this->set_block('ihtml',$blockname,$this->handelcount);

$this->handelcount ;
return
true;
}

//example : array
// $data = array (
// '0' => array('1','2','3'),
// '1' => array('4','5','6'),
// );
// or var
function SetBlockVar($data,$blockname,$var = '') {
if(
is_array($data)) {
$x = count($data);
$y = count($data[0]);

for(
$i = 0 ; $i < $x ; $i ) {
for(
$j = 0 ; $j < $y ; $j ) {
$this->set_var('var'.$j,$data[$i][$j]);
}
$this->parse($this->handelkey[$blockname],$blockname,true);
}
} else {
$this->set_var($var,$data);
}
return
true;
}

function
BlockParse($blockname) {
if(!empty(
$blockname)) {
$this->parse($this->handelkey[$blockname],$blockname,true);
return
true;
}
return
false;
}

}
#end Template class

/******************** CODE END ********************/
}
?>



调用的方法
$t = new template('test.tpl');
$t->SetVar('test','中国');//替换相关的字串。
$t->OP();

如果是用这样的一个模板
<table>
<!-- BEGIN list -->
<tr>
<td>{var0}</td>
<td>{var1}</td>
</tr>
<!-- END list -->
</table>

<table>
<!-- BEGIN list2 -->
<tr>
<td>{var0}</td>
<td>{var1}</td>
</tr>
<!-- END list2 -->
</table>

则这样用
<?
include("template.h.php");
include('template.h.php');
$data=array();
$data['list']=array(
'0'=>array('a1','a2'),
'1'=>array('b1','b2') <#006600>1
undefined -->", $str);
break;
}

return
$str;
}

/* public: p(string $varname)
* varname: name of variable to print.
*/
function p($varname) {
echo
$this->finish($this->get_var($varname));
}

function
get($varname) {
return
$this->finish($this->get_var($varname));
}

/***************************************************************************/
/* private: filename($filename)
* filename: name to be completed.
*/
function filename($filename) {
if (
substr($filename, 0, 1) != "/") {
$filename = $this->root."/".$filename;
}

if (!
file_exists($filename))
$this->halt("filename: file $filename does not exist.");

return
$filename;
}

/* private: varname($varname)
* varname: name of a replacement variable to be protected.
*/
function varname($varname) {
return
preg_quote("{".$varname."}");
}

/* private: loadfile(string $handle)
* handle: load file defined by handle, if it is not loaded yet.
*/
function loadfile($handle) {
if (isset(
$this->varkeys[$handle]) and !empty($this->varvals[$handle]))
return
true;

if (!isset(
$this->file[$handle])) {
$this->halt("loadfile: $handle is not a valid handle.");
return
false;
}
$filename = $this->file[$handle];

$str = implode("", @file($filename));
if (empty(
$str)) {
$this->halt("loadfile: While loading $handle, $filename does not exist or is empty.");
return
false;
}

$this->set_var($handle, $str);

return
true;
}

/***************************************************************************/
/* public: halt(string $msg)
* msg: error message to show.
*/
function halt($msg) {
$this->last_error = $msg;

if (
$this->halt_on_error != "no")
$this->haltmsg($msg);

if (
$this->halt_on_error == "yes")
die(
"<b>Halted.</b>");

return
false;
}

/* public, override: haltmsg($msg)
* msg: error message to show.
*/
function haltmsg($msg) {
printf("<b>Template Error:</b> %s<br>n", $msg);
}
}
#end Template_base class

//类名:Template
//功能:模板处理扩展
//说明:继承于Tempalte_base,修改了部分的模板处理函数
class Template extends Template_base {
var
$handelkey = array();
var
$handelcount;

function
Template($filename) {
$this->Template_base();
if(empty(
$filename) || !file_exists($filename)) {
die(
"Template -> Template() : Error - file $filename does not exist");
}
$this->set_file('ihtml',$filename);
$this->handelcount = 1;
return
true;
}

function
Output() {
$this->p('out');
return
true;
}

function
Compile() {
$this->parse('out','ihtml');
return
true;
}

function
OP() {
$copyright = '<p align=center>&copy; 2002 new Maya workroom</p>';
$this->SetVar('copyright',$copyright);
$this->Compile();
$this->OutPut();
return
true;
}

//example: var or array
// $key = array(
// 'row1' => '序号',
// 'row2' => '姓名',
// 'row3' => '性别'
// )
function SetVar($key,$value = '') {
$this->set_var($key,$value);
return
true;
}

// $key is define the current block
function SetBlock($blockname) {
$this->handelkey[$blockname] = $this->handelcount;
$this->set_block('ihtml',$blockname,$this->handelcount);

$this->handelcount ;
return
true;
}

//example : array
// $data = array (
// '0' => array('1','2','3'),
// '1' => array('4','5','6'),
// );
// or var
function SetBlockVar($data,$blockname,$var = '') {
if(
is_array($data)) {
$x = count($data);
$y = count($data[0]);

for(
$i = 0 ; $i < $x ; $i ) {
for(
$j = 0 ; $j < $y ; $j ) {
$this->set_var('var'.$j,$data[$i][$j]);
}
$this->parse($this->handelkey[$blockname],$blockname,true);
}
} else {
$this->set_var($var,$data);
}
return
true;
}

function
BlockParse($blockname) {
if(!empty(
$blockname)) {
$this->parse($this->handelkey[$blockname],$blockname,true);
return
true;
}
return
false;
}

}
#end Template class

/******************** CODE END ********************/
}
?>



调用的方法
$t = new template('test.tpl');
$t->SetVar('test','中国');//替换相关的字串。
$t->OP();

如果是用这样的一个模板
<table>
<!-- BEGIN list -->
<tr>
<td>{var0}</td>
<td>{var1}</td>
</tr>
<!-- END list -->
</table>

<table>
<!-- BEGIN list2 -->
<tr>
<td>{var0}</td>
<td>{var1}</td>
</tr>
<!-- END list2 -->
</table>

则这样用
<?
include("template.h.php");
include('template.h.php');
$data=array();
$data['list']=array(
'0'=>array('a1','a2'),
'1'=>array('b1','b2')
);
$data['list2']=array(
'0'=>array('aa1','aa2'),
'1'=>array('bb1','bb2')
);
$t = new template('test.tpl');
$t->SetBlock('list');
$t->SetBlockVar($data['list'],'list');
$t->SetBlock('list2');
$t->SetBlockVar($data['list2'],'list2');
$t->OP();
?>

其它的方法大家再研究一下吧。如果有兴趣的话,可以帮我扩充一下。呵呵
分页 [1] [2] [3] [4] [5] [6] [7] [8]
master8
  • 上一篇:大家关心的问题,开发短信程序(java)
  • 下一篇:Skype出台海外赔偿方案

  • 我要投稿  打印本文  推荐本文  加入收藏  返回顶部  关闭窗口
    搜模板(www.somoban.com) 原创网站模板交易平台
    阿里妈妈再掀疯狂采购风,网站广告位严重告急,急召天下站长
    基于PHP+MySQL的整站、模块、插件开发等或者按需求实现相应功能;
基于各PHP主流建站系统CMS,BBS,BLOG等的模板定制,完全手写代码;
整站数据迁移或备份恢复;网页代码优化、重构;整站常规SEO优化;网站技术支持;
点击了解详情...
    站长论坛
    • 验证码: