文件1:classlist.html
current time is : _now_ <BR><BR>
current school class list :
<TABLE border=1>
<TR>
<TH>ID</TH>
<TH>NAME</TH>
<TH>GRADE</TH>
<TH>CLASS</TH>
</TR>
BEGIN_classlist_
<TR>
<TD>_cid_</TD>
<TD>_cname_</TD>
<TD>_grade_</TD>
<TD>_class_</TD>
</TR>
END_classlist_
</TABLE>
文件2:studentlist.html
current time is : _now_ <BR><BR>
current class is :
<TABLE border=1>
BEGIN_classinfo_
<TR>
<TH>class id: _cid_</TH>
<TH>class name: _cname_</TH>
<TH>class grade: _grade_</TH>
<TH>class num: _class_</TH>
</TR>
END_classinfo_
</TABLE><br>
current class's student :
<TABLE border=1>
<TR>
<TH>ID</TH>
<TH>NAME</TH>
<TH>SCORE</TH>
</TR>
BEGIN_studentlist_
<TR>
<TD>_sid_</TD>
<TD>_sname_</TD>
<TD>_score_</TD>
</TR>
END_studentlist_
</TABLE>
文件3:TemplateParser.php
上面的模板解析类是笔者一时写的一个复杂的模板解析类,功效很少,没有真正甚么利用价值。然而在这里可以知足这篇文章解说的需求。
同时,假如之前没有接触过模板解析,对模板解析的完成办法有必定疑问的同寅,可以研讨一下这个复杂类完成解析的体例,代码挺复杂的应当能看懂的。
这个解析类有本人的模板布局,“块”(要停止轮回显示的中央)的界说以下:
BEGIN_你的块名_
......html代码.......
_你的块外面的变量的称号_
......html代码.......
END_你的块名_
变量的界说体例以下:
......html代码.......
_你的块外面的变量的称号_
......html代码.......
详细的“块”和“变量”的利用参考下面的两个模板
<?php
/*
* template parse class
*/
class TemplateParser{
/*
* @var template root directory
*/
var $root = '';
/*
* @var template filename
*/
var $tpl = '';
/*
* @var data for parse template
*/
var $data = array();
/*
* @var template parse result
*/
var $result = '';
/*
* construct function
*/
function TemplateParser($root=''){
$this->root = $root;
}
/*
* set template file name
*/
function loadTemplateFile($tplFile){
$this->tpl = $tplFile;
}
/*
* set global var value;
*
* @param $varName global var name
* @param $data var's value
*/
function setData($varName, $data){
$this->data['__ALL__'][$varName] = $data;
}
/*
* set global var value;
*
* @param $blockName template block name
* @param $data var value
* @param $rec var value
*/
function setBlockData($blockName, &$data, $rec=false){
$this->data[$blockName] = &$data;
$this->rec[$blockName] = $rec;
}
/*
* parse template action
*/
function parse(){
$tplstr = file_get_contents("{$this->root}/{$this->tpl}");
foreach($this->data as $block=>$value){
$tag = "|BEGIN_{$block}_(.*)END_{$block}_|sm";
preg_match($tag, $tplstr, $tmpdata);
if($tmpdata[1]!=null){
$tmpstr = '';
if($this->rec[$block]){
foreach($value as $subValue)
$tmpstr .= $this->_parseBlock($tmpdata[1], $subValue);
}else{
$tmpstr .= $this->_parseBlock($tmpdata[1], $value);
}
}
$tplstr = preg_replace("|BEGIN_{$block}_(.*)END_{$block}_|sm", $tmpstr, $tplstr);
}
$tplstr = $this->_parseBlock($tplstr, $this->data['__ALL__']);
$this->result = $tplstr;
}
/*
* parse block
*
* @param $str string one block string
* @param $data array data for parse
*/
function _parseBlock($str, $data){
foreach($data as $key=>$value){
$keys[] = "_{$key}_";
$values[] = "$value";
}
return str_replace($keys, $values, $str);
}
/*
* return parse result
*/
function get(){
return $this->result;
}
/*
* show parse result
*/
function show(){
echo $this->result;
}
}
?>
文件4:Render.php
<?php
/*
* include template parser class
*/
require_once "TemplateParser.php";
/*
* base render class
*/
class Render{
/*
* @var object of template parser class
*/
var $parser = "";
/*
* instruct function
*/
function Render($root, $tplFile){
$this->parser = new TemplateParser($root);
$this->parser->loadTemplateFile($tplFile);
}
/*
* add data to template parser
*
* @param $data array() data for parse
*/
function initData(&$data){
return ;
}
/*
* show template parse result
*/
function show(){
$this->parser->parse();
$this->parser->show();
}
}
?>
文件5:StudentRender.php
<?php
/*
* include base class
*/
require_once "Render.php";
/*
* shudent list show render
*/
class StudentRender extends Render{
function StudentRender(){
parent::Render('./template', 'studentlist.html');
}
function initData($data){
$this->parser->setData('now', date('Y-m-d H:i:s'));
$this->parser->setBlockData('classinfo', $data['class'], false);
$this->parser->setBlockData('studentlist', $data['student'], true);
}
}
?>
文件6:ClassRender.php
<?php
/*
* include base class
*/
require_once "Render.php";
/*
* class list show render
*/
class ClassRender extends Render{
function ClassRender(){
parent::Render('./template', 'classlist.html');
}
function initData(&$data){
$this->parser->setData('now', date('Y-m-d H:i:s'));
$this->parser->setBlockData('classlist', &$data['class'], true);
}
}
?>
--------------------------------------------------
上面两个是测试文件,第一个对照复杂一些
--------------------------------------------------
测试文件1:ClassRenderTest.php
<?php
/*
* include "class list show render"
*/
require_once "render/ClassRender.php";
/*
* data for parse
*
* include (class list)
*/
$data = array('class'=>array('1'=>array('cid'=>1, 'cname'=>'class one', 'grade'=>3, 'class'=>1),
'2'=>array('cid'=>2, 'cname'=>'class two', 'grade'=>3, 'class'=>2),
'3'=>array('cid'=>3, 'cname'=>'class three', 'grade'=>4, 'class'=>1),
'4'=>array('cid'=>4, 'cname'=>'class four', 'grade'=>4, 'class'=>2),
'5'=>array('cid'=>5, 'cname'=>'class five', 'grade'=>5, 'class'=>1)));
/*
* do template parse
*/
doRender($data);
/*
* may see as controler's action, use to parse template
*/
function doRender(&$data){
$render = new ClassRender();
$render->initData($data);
$render->show();
}
?>
运转了局:
current time is : 2004-05-10 23:51:26 <BR><BR>
current school class list :
<TABLE border=1>
<TR>
<TH>ID</TH>
<TH>NAME</TH>
<TH>GRADE</TH>
<TH>CLASS</TH>
</TR>
<TR>
<TD>1</TD>
<TD>class one</TD>
<TD>3</TD>
<TD>1</TD>
</TR>
<TR>
<TD>2</TD>
<TD>class two</TD>
<TD>3</TD>
<TD>2</TD>
</TR>
<TR>
<TD>3</TD>
<TD>class three</TD>
<TD>4</TD>
<TD>1</TD>
</TR>
<TR>
<TD>4</TD>
<TD>class four</TD>
<TD>4</TD>
<TD>2</TD>
</TR>
<TR>
<TD>5</TD>
<TD>class five</TD>
<TD>5</TD>
<TD>1</TD>
</TR>
</TABLE>
测试文件2:StudentRenderTest.php
<?php
/*
* include "shudent list show render"
*/
require_once "render/StudentRender.php";
/*
* data for parse
*
* include (one class info) and (one class's student list)
*/
$data = array('class'=>array('cid'=>1, 'cname'=>'class one', 'grade'=>3, 'class'=>1),
'student'=>array('1'=>array('sid'=>1, 'sname'=>'stu one', 'score'=>100),
'2'=>array('sid'=>2, 'sname'=>'stu two', 'score'=>90),
'3'=>array('sid'=>3, 'sname'=>'stu three', 'score'=>80),
'4'=>array('sid'=>4, 'sname'=>'stu four', 'score'=>95),
'5'=>array('sid'=>5, 'sname'=>'stu five', 'score'=>55)));
/*
* do template parse
*/
doRender($data);
/*
* may see as controler's action, use to parse template
*/
function doRender(&$data){
$render = new StudentRender();
$render->initData($data);
$render->show();
}
?>
运转了局:
current time is : 2004-05-10 23:52:22 <BR><BR>
current class is :
<TABLE border=1>
<TR>
<TH>class id: 1</TH>
<TH>class name: class one</TH>
<TH>class grade: 3</TH>
<TH>class num: 1</TH>
</TR>
</TABLE><br>
current class's student :
<TABLE border=1>
<TR>
<TH>ID</TH>
<TH>NAME</TH>
<TH>SCORE</TH>
</TR>
<TR>
<TD>1</TD>
<TD>stu one</TD>
<TD>100</TD>
</TR>
<TR>
<TD>2</TD>
<TD>stu two</TD>
<TD>90</TD>
</TR>
<TR>
<TD>3</TD>
<TD>stu three</TD>
<TD>80</TD>
</TR>
<TR>
<TD>4</TD>
<TD>stu four</TD>
<TD>95</TD>
</TR>
<TR>
<TD>5</TD>
<TD>stu five</TD>
<TD>55</TD>
</TR>
</TABLE>