MVC模式的view层的主要任务是进行页面的和结果的显示工作,在php的实现过程中,主要是体现为一个模板(使用模板,可以达到php代码和html代码分离的目的,这样代码和页面的维护就方便多了,便于管理和页面的更换,可以真正的划分程序员、美工的分工)的解析过程:
首先,controler层从model层得到数据
其次,controler层将数据交给view层
再次,view层的接口将数据按一定的方式传给模板解析类,
最后,模板解析类将数据解析到模板中,然后显示。
下面是一个具体的实现例子
目录结构
|- ClassRenderTest.php //测试解析classlist.html
|- StudentRenderTest.php //测试解析studentlist.html
|- render / TemplateParser.php //模板解析类
|- render / Render.php //解析模板的所有类的基础类
|- render / StudentRender.php //解析模板studentlist.html的类
|- render / ClassRender.php //解析模板classlist.html的类
|- template / studentlist.html //模板文件
|- template / classlist.html //模板文件
注意:
1、这里模板解析类选用了简单的“TemplateParser.php”,根据个人的需要你可以选用任何一种模板解析类;
2、如果每个模板解析都直接调用“TemplateParser.php”,可能会有大量的重复代码出现,这是oo思想所不准许出现的。因此采用“Render.php”对它进行包装,然后再对“Render.php”里面的Render类进行扩展,来对不同文件模板进行解析;
3、不同的模板解析类的,使用的方法是不同的,他们的包装方式也可能不同。
4、“StudentRender.php”“ClassRender.php”就是包装过的Render类,分别用来满足解析“studentlist.html”“classlist.html”的需要。
文件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>