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>
新闻热点
疑难解答