| 一个易用的模板类 |
|
来源:互联网 |
时间: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>© 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>© 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] |
|
 |
| |
|
|
|
|