|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
我先把我自己学习PHP的过程做一下概括: 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>
建议大家买一本书,而不光是在网上看一些零碎的资料,一本书毕竟会讲的系统一些,全面一些,而且印刷的书不受电脑的限制,但是建议在看书的时候最好旁边有电脑,这样可以很及时地上机实践。 |
|