首页 后端开发 php教程 mvc模式 PHP中MVC模式的模板引擎开发经验分享

mvc模式 PHP中MVC模式的模板引擎开发经验分享

Jul 29, 2016 am 08:44 AM
mvc模式

使Web系统的开发与维护更加方便,从而有效的节省人力物力,受到了越来越多企业的青眯。
模板引擎是MVC模式建立过程的重要方法,开发者可以设计一套赋予含义的标签,通过技术解析处理有效的把数据逻辑处理从界面模板中提取出来,通过解读标签的含义把控制权提交给相应业务逻辑处理程序,从而获取到需要的数据,以模板设计的形式展现出来,使设计人员能把精力更多放在表现形式上。下面是我对模板引擎的认识与设计方法:
说的好听些叫模板引擎,实际就是解读模板数据的过程(个人观点^^)。通过我对建站方面的思考认识,网站在展现形式上无非归纳为单条和多条两种形式,那么我们可以设定两种对应标签(如data、list)来处理这两种情况,关键点在于解决两种标签的多层相互嵌套问题,基本适合实现80%界面形式。
解读模板的方法有多种,常用的包括字符串处理(解决嵌套稍麻烦)、正则表达式。在这里我选用的正则表达式,下面是我的处理方法(本文仅提供思路和参考代码,可能不能直接使用)。
模板文件解析类:

复制代码 代码如下:


/*
* class: 模板解析类
* author: 51JS.COM-ZMM
* date: 2011.3.1
* email: 304924248@qq.com
* blog: http://www.cnblogs.com/cnzmm/
*/
class Template {
public $html, $vars, $bTag, $eTag;
public $bFlag='{', $eFlag='}', $pfix='zmm:';
private $folder, $file;
function __construct($vars=array()) {
!empty($vars) && $this->vars = $vars;
!empty($GLOBALS['cfg_tag_prefix']) &&
$this->pfix = $GLOBALS['cfg_tag_prefix'].':';
$this->bTag = $this->bFlag.$this->pfix;
$this->eTag = $this->bFlag.'\/'.$this->pfix;
empty(Tags::$vars) && Tags::$vars = &$this->vars;
}
public function LoadTpl($tpl) {
$this->file = $this->GetTplPath($tpl);
Tags::$file = &$this->file;
if (is_file($this->file)) {
if ($this->GetTplHtml()) {
$this->SetTplTags();
} else {
exit('模板文件加载失败!');
}
} else {
exit('模板文件['.$this->file.']不存在!');
}
}
private function GetTplPath($tpl) {
$this->folder = WEBSITE_DIRROOT.
$GLOBALS['cfg_tpl_root'];
return $this->folder.'/'.$tpl;
}
private function GetTplHtml() {
$html = self::FmtTplHtml(file_get_contents($this->file));
if (!empty($html)) {
$callFunc = Tags::$prefix.'Syntax';
$this->html = Tags::$callFunc($html, new Template());
} else {
exit('模板文件内容为空!');
} return true;
}
static public function FmtTplHtml($html) {
return preg_replace('/(\r)|(\n)|(\t)|(\s{2,})/is', '', $html);
}
public function Register($vars=array()) {
if (is_array($vars)) {
$this->vars = $vars;
Tags::$vars = &$this->vars;
}
}
public function Display($bool=false, $name="", $time=0) {
if (!empty($this->html)) {
if ($bool && !empty($name)) {
if (!is_int($time)) $time = 600;
$cache = new Cache($time);
$cache->Set($name, $this->html);
}
echo $this->html; flush();
} else {
exit('模板文件内容为空!');
}
}
public function SetAssign($souc, $info) {
if (!empty($this->html)) {
$this->html = str_ireplace($souc, self::FmtTplHtml($info), $this->html);
} else {
exit('模板文件内容为空!');
}
}
private function SetTplTags() {
$this->SetPanelTags(); $this->SetTrunkTags(); $this->RegHatchVars();
}
private function SetPanelTags() {
$rule = $this->bTag.'([^'.$this->eFlag.']+)\/'.$this->eFlag;
preg_match_all('/'.$rule.'/ism', $this->html, $out_matches);
$this->TransTag($out_matches, 'panel'); unset($out_matches);
}
private function SetTrunkTags() {
$rule = $this->bTag.'(\w+)\s*([^'.$this->eFlag.']*?)'.$this->eFlag.
'((?:(?!'.$this->bTag.')[\S\s]*?|(?R))*)'.$this->eTag.'\\1\s*'.$this->eFlag;
preg_match_all('/'.$rule.'/ism', $this->html, $out_matches);
$this->TransTag($out_matches, 'trunk'); unset($out_matches);
}
private function TransTag($result, $type) {
if (!empty($result[0])) {
switch ($type) {
case 'panel' : {
for ($i = 0; $i $strTag = explode(' ', $result[1][$i], 2);
if (strpos($strTag[0], '.')) {
$itemArg = explode('.', $result[1][$i], 2);
$callFunc = Tags::$prefix.ucfirst($itemArg[0]);
if (method_exists('Tags', $callFunc)) {
$html = Tags::$callFunc(chop($itemArg[1]));
if ($html !== false) {
$this->html = str_ireplace($result[0][$i], $html, $this->html);
}
}
} else {
$rule = '^([^\s]+)\s*([\S\s]+)$';
preg_match_all('/'.$rule.'/is', trim($result[1][$i]), $tmp_matches);
$callFunc = Tags::$prefix.ucfirst($tmp_matches[1][0]);
if (method_exists('Tags', $callFunc)) {
$html = Tags::$callFunc($tmp_matches[2][0]);
if ($html !== false) {
$this->html = str_ireplace($result[0][$i], $html, $this->html);
}
} unset($tmp_matches);
}
} break;
}
case 'trunk' : {
for ($i = 0; $i $callFunc = Tags::$prefix.ucfirst($result[1][$i]);
if (method_exists('Tags', $callFunc)) {
$html = Tags::$callFunc($result[2][$i], $result[3][$i]);
$this->html = str_ireplace($result[0][$i], $html, $this->html);
}
} break;
}
default: break;
}
} else {
return false;
}
}
private function RegHatchVars() {
$this->SetPanelTags();
}
function __destruct() {}
}
?>


标签解析类:(目前暂时提供data、list两种标签的解析,说明思路)

复制代码 代码如下:


/*
* class: 标签解析类
* author: 51JS.COM-ZMM
* date: 2011.3.2
* email: 304924248@qq.com
* blog: http://www.cnblogs.com/cnzmm/
*/
class Tags {
static private $attrs=null;
static public $file, $vars, $rule, $prefix='TAG_';
static public function TAG_Syntax($html, $that) {
$rule = $that->bTag.'if\s+([^'.$that->eFlag.']+)\s*'.$that->eFlag;
$html = preg_replace('/'.$rule.'/ism', '', $html);
$rule = $that->bTag.'elseif\s+([^'.$that->eFlag.']+)\s*'.$that->eFlag;
$html = preg_replace('/'.$rule.'/ism', '', $html);
$rule = $that->bTag.'else\s*'.$that->eFlag;
$html = preg_replace('/'.$rule.'/ism', '', $html);
$rule = $that->bTag.'loop\s+(\S+)\s+(\S+)\s*'.$that->eFlag;
$html = preg_replace('/'.$rule.'/ism', '', $html);
$rule = $that->bTag.'loop\s+(\S+)\s+(\S+)\s+(\S+)\s*'.$that->eFlag;
$html = preg_replace('/'.$rule.'/ism', ' \\3) { ?>', $html);
$rule = $that->eTag.'(if|loop)\s*'.$that->eFlag;
$html = preg_replace('/'.$rule.'/ism', '', $html);
$rule = $that->bTag.'php\s*'.$that->eFlag.'((?:(?!'.
$that->bTag.')[\S\s]*?|(?R))*)'.$that->eTag.'php\s*'.$that->eFlag;
$html = preg_replace('/'.$rule.'/ism', '', $html);
return self::TAG_Execute($html);
}
static public function TAG_List($attr, $html) {
if (!empty($html)) {
if (self::TAG_HaveTag($html)) {
return self::TAG_DealTag($attr, $html, true);
} else {
return self::TAG_GetData($attr, $html, true);
}
} else {
exit('标签{list}的内容为空!');
}
}
static public function TAG_Data($attr, $html) {
if (!empty($html)) {
if (self::TAG_HaveTag($html)) {
return self::TAG_DealTag($attr, $html, false);
} else {
return self::TAG_GetData($attr, $html, false);
}
} else {
exit('标签{data}的内容为空!');
}
}
static public function TAG_Execute($html) {
ob_clean(); ob_start();
if (!empty(self::$vars)) {
is_array(self::$vars) &&
extract(self::$vars, EXTR_OVERWRITE);
}
$file_inc = WEBSITE_DIRINC.'/buffer/'.
md5(uniqid(rand(), true)).'.php';
if ($fp = fopen($file_inc, 'xb')) {
fwrite($fp, $html);
if (fclose($fp)) {
include($file_inc);
$html = ob_get_contents();
} unset($fp);
} else {
exit('模板解析文件生成失败!');
} ob_end_clean(); @unlink($file_inc);
return $html;
}
static private function TAG_HaveTag($html) {
$bool_has = false;
$tpl_ins = new Template();
self::$rule = $tpl_ins->bTag.'([^'.$tpl_ins->eFlag.']+)\/'.$tpl_ins->eFlag;
$bool_has = $bool_has || preg_match('/'.self::$rule.'/ism', $html);
self::$rule = $tpl_ins->bTag.'(\w+)\s*([^'.$tpl_ins->eFlag.']*?)'.$tpl_ins->eFlag.
'((?:(?!'.$tpl_ins->bTag.')[\S\s]*?|(?R))*)'.$tpl_ins->eTag.'\\1\s*'.$tpl_ins->eFlag;
$bool_has = $bool_has || preg_match('/'.self::$rule.'/ism', $html);
unset($tpl_ins);
return $bool_has;
}
static private function TAG_DealTag($attr, $html, $list) {
preg_match_all('/'.self::$rule.'/ism', $html, $out_matches);
if (!empty($out_matches[0])) {
$child_node = array();
for ($i = 0; $i $child_node[] = $out_matches[3][$i];
$html = str_ireplace($out_matches[3][$i], '{-->>child_node_'.$i.'}
$html = self::TAG_GetData($attr, $html, $list);
for ($i = 0; $i $html = str_ireplace('{-->>child_node_'.$i.'}
preg_match_all('/'.self::$rule.'/ism', $html, $tmp_matches);
if (!empty($tmp_matches[0])) {
for ($i = 0; $i $callFunc = self::$prefix.ucfirst($tmp_matches[1][$i]);
if (method_exists('Tags', $callFunc)) {
$temp = self::$callFunc($tmp_matches[2][$i], $tmp_matches[3][$i]);
$html = str_ireplace($tmp_matches[0][$i], $temp, $html);
}
}
}
unset($tmp_matches);
}
unset($out_matches); return $html;
}
static private function TAG_GetData($attr, $html, $list=false) {
if (!empty($attr)) {
$attr_ins = new Attbt($attr);
$attr_arr = $attr_ins->attrs;
if (is_array($attr_arr)) {
extract($attr_arr, EXTR_OVERWRITE);
$source = table_name($source, $column);
$rule = '\[field:\s*(\w+)\s*([^\]]*?)\s*\/?]';
preg_match_all('/'.$rule.'/is', $html, $out_matches);
$data_str = '';
$data_ins = new DataSql();
$attr_where = $attr_order = '';
if (!empty($where)) {
$where = str_replace(',', ' and ', $where);
$attr_where = ' where '. $where;
}
if (!empty($order)) {
$attr_order = ' order by '.$order;
} else {
$fed_name = '';
$fed_ins = $data_ins->GetFedNeedle($source);
$fed_cnt = $data_ins->GetFedCount($fed_ins);
for ($i = 0; $i $fed_flag = $data_ins->GetFedFlag($fed_ins, $i);
if (preg_match('/auto_increment/ism', $fed_flag)) {
$fed_name = $data_ins->GetFedName($fed_ins, $i);
break;
}
}
if (!empty($fed_name))
$attr_order = ' order by '.$fed_name.' desc';
}
if ($list == true) {
if (empty($source) && empty($sql)) {
exit('标签{list}必须指定source属性!');
}
$attr_rows = $attr_page = '';
if ($rows > 0) {
$attr_rows = ' limit 0,'.$rows;
}
if (!empty($sql)) {
$data_sql = $sql;
} else {
$data_sql = 'select * from `'.$source.'`'.
$attr_where.$attr_order.$attr_rows;
}
if ($pages=='true' && !empty($size)) {
$data_num = $data_ins->GetRecNum($data_sql);
$page_cnt = ceil($data_num / $size);
global $page;
if (!isset($page) || $page if ($page > $page_cnt) $page = $page_cnt;
$data_sql = 'select * from `'.$source.'`'.$attr_where.
$attr_order.' limit '.($page-1) * $size.','.$size;
$GLOBALS['cfg_page_curr'] = $page;
$GLOBALS['cfg_page_prev'] = $page - 1;
$GLOBALS['cfg_page_next'] = $page + 1;
$GLOBALS['cfg_page_nums'] = $page_cnt;
if (function_exists('list_pagelink')) {
$GLOBALS['cfg_page_list'] = list_pagelink($page, $page_cnt, 2);
}
}
$data_idx = 0;
$data_ret = $data_ins->SqlCmdExec($data_sql);
while ($row = $data_ins->GetRecArr($data_ret)) {
if ($skip > 0 && !empty($flag)) {
$data_idx != 0 &&
$data_idx % $skip == 0 &&
$data_str .= $flag;
}
$data_tmp = $html;
$data_tmp = str_ireplace('@idx', $data_idx, $data_tmp);
for ($i = 0; $i $data_tmp = str_ireplace($out_matches[0][$i],
$row[$out_matches[1][$i]], $data_tmp);
}
$data_str .= $data_tmp; $data_idx ++;
}
} else {
if (empty($source)) {
exit('标签{data}必须指定source属性!');
}
$data_sql = 'select * from `'.$source.
'`'.$attr_where.$attr_order;
$row = $data_ins->GetOneRec($data_sql);
if (is_array($row)) {
$data_tmp = $html;
for ($i = 0; $i $data_val = $row[$out_matches[1][$i]];
if (empty($out_matches[2][$i])) {
$data_tmp = str_ireplace($out_matches[0][$i], $data_val, $data_tmp);
} else {
$attr_str = $out_matches[2][$i];
$attr_ins = new Attbt($attr_str);
$func_txt = $attr_ins->attrs['function'];
if (!empty($func_txt)) {
$func_tmp = explode('(', $func_txt);
if (function_exists($func_tmp[0])) {
eval('$func_ret ='.str_ireplace('@me',
'\''.$data_val.'\'', $func_txt));
$data_tmp = str_ireplace($out_matches[0][$i], $func_ret, $data_tmp);
} else {
exit('调用了不存在的函数!');
}
} else {
exit('标签设置属性无效!');
}
}
}
$data_str .= $data_tmp;
}
}
unset($data_ins);
return $data_str;
} else {
exit('标签设置属性无效!');
}
} else {
exit('没有设置标签属性!');
}
}
static public function __callStatic($name, $args) {
exit('标签{'.$name.'}不存在!');
}
}
?>

以上就介绍了mvc模式 PHP中MVC模式的模板引擎开发经验分享,包括了mvc模式方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

PHP开发:使用 CodeIgniter 实现 MVC 模式和 RESTful API PHP开发:使用 CodeIgniter 实现 MVC 模式和 RESTful API Jun 16, 2023 am 08:09 AM

随着Web应用程序的不断发展,更加快速和高效地开发应用程序变得非常重要。并且,随着RESTfulAPI在Web应用程序中的广泛应用,对于开发人员来说,必须理解如何创建和实现RESTfulAPI。在本文中,我们将讨论如何使用CodeIgniter框架实现MVC模式和RESTfulAPI。MVC模式简介MVC(Model-Vie

了解Laravel能实现的功能和特点 了解Laravel能实现的功能和特点 Mar 07, 2024 am 08:06 AM

Laravel是一款流行的PHP框架,被广泛应用于Web开发领域。它提供了许多强大的功能和特点,使得开发人员可以快速构建高质量、可维护的Web应用程序。在本文中,我们将深入探讨Laravel能实现的功能和特点,并提供具体的代码示例说明。1.MVC架构Laravel采用了基于MVC(Model-View-Controller)设计模式的架构,使代码的组织和管

在ThinkPHP6中使用MVC模式 在ThinkPHP6中使用MVC模式 Jun 20, 2023 am 11:06 AM

ThinkPHP6是一个流行的PHP框架,它使用MVC(Model-View-Controller)模式来组织应用程序的逻辑结构。MVC模式是一种用于面向对象编程的设计模式,它将应用程序分为三个部分:模型(Model)、视图(View)和控制器(Controller)。每个部分都有其独立的职责,使得应用程序易于维护和扩展。本文将介绍如何在ThinkPHP6中

如何在PHP框架中实现MVC架构模式? 如何在PHP框架中实现MVC架构模式? Jun 05, 2023 pm 03:31 PM

随着互联网技术的飞速发展,各种编程语言和框架也不断涌现,PHP作为一种全球流行的编程语言,被广泛应用于Web开发领域。随着Web应用的不断增加和复杂化,为了更好的组织和管理代码,开发人员采用了MVC(Model-View-Controller)架构模式。MVC模式是一种软件设计模式,将应用程序分成三个部分:数据(Model)、用户界面(View)和控制器(C

为什么选择CodeIgniter框架? 为什么选择CodeIgniter框架? Jun 03, 2023 am 08:21 AM

CodeIgniter是一个简单、轻量级的PHP框架,被广泛用于快速开发Web应用程序。自从2006年问世以来,CodeIgniter一直是许多开发者的首选框架。在这篇文章中,我们将探讨为什么选择CodeIgniter框架,并讨论其主要优点。轻量级CodeIgniter是一个轻量级框架,框架核心文件只有2MB左右,这让它在运行速度和性能方面表现出色。与其他庞

PHP中的请求响应设计 PHP中的请求响应设计 May 23, 2023 am 08:22 AM

在Web开发中,请求响应是非常重要的概念。对于PHP开发者来说,了解请求响应的设计是十分必要的。在PHP中,请求指客户端发送给服务器的数据。一般而言,这些数据是由表单提交或者AJAX请求所发出的。请求的内容可以包含HTTP请求方法、请求头部、请求参数、请求体等信息。而响应则代表服务器对客户端请求的返回结果。一般而言,响应的内容可能包括H

PHP8框架开发MVC模式:提高团队效率的实用方法 PHP8框架开发MVC模式:提高团队效率的实用方法 Sep 11, 2023 pm 07:13 PM

PHP8框架开发MVC模式:提高团队效率的实用方法在当今的软件开发中,MVC(Model-View-Controller)模式被广泛应用于Web应用程序开发中。它将应用程序的逻辑部分分解为三个组件:模型、视图和控制器,以实现高内聚和低耦合的架构。而PHP8提供了更多的特性和改进,使得MVC模式在框架开发中的应用更加高效和方便。本文将介绍如何使用PHP8框架开

PHP8框架开发MVC模式:解决挑战的实用建议 PHP8框架开发MVC模式:解决挑战的实用建议 Sep 11, 2023 pm 01:25 PM

【导言】MVC(Model-View-Controller)模式在Web应用程序开发中被广泛应用。PHP是一种非常流行的编程语言,也拥有许多优秀的开发框架,如Laravel和Symfony等。随着PHP8的发布,开发者对于如何在PHP8中合理地开发MVC模式的应用程序提出了一些挑战。本文将为大家提供一些实用的建议,以帮助开发者克服这些挑战。【一、PHP8的新

See all articles