首页 / PHP / 一个简单的PHP模板引擎
一个简单的PHP模板引擎
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了一个简单的PHP模板引擎,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含15582字,纯文字阅读大概需要23分钟。
内容图文
PHP早期开发中通常是PHP代码和HTML代码混写,这也使代码中充斥着数据库操作,逻辑处理等。当项目不大时,这样的代码还可以接受,但是随着项目不断扩大,我们就会发现同一个文件中同时存在前端逻辑和后端处理,当逻辑越来越复杂时,代码的可读性和可维护性都会变得非常差,以至于后来不得不进行大规模的代码重构。所以后来就出现了代码分层的思想,尽量拆分开前端代码和后端代码。
PHP模板引擎能解决这种混乱吗?当然可以。但是呢,即使你不用专门的模板引擎也可以写出逻辑清晰的代码,重点是要有分层的思想,有专门的脚本去进行数据获取,数据处理,逻辑处理等,在展示页面只进行尽可能简单的逻辑处理即可。既然这样,那还有使用PHP模板引擎的必要吗?毫无疑问当然有,因为模板引擎的功能不仅于此。
那接下来就说一下PHP模板引擎的主要作用:
1、它实现了一些自定义标签,用于展示层的简单逻辑处理,相较于不适用引擎的好处是代码看起来不像是PHP代码了,感觉上HTML代码和PHP代码完全分开了,但这只是假象,坏处是效率降低了,因为这样的页面需要专门的脚本解析后才能正确显示,解析的方法就是使用正则表达式替换,这明显降低了效率。到现在来看感觉PHP模板引擎有还不如没有呢,那为什么还要用它呢,重点是他的下一个功能。
2、文件缓存,这是模板引擎在生产环境中提高效率的非常好的手段。可以用在页面加载时所用数据量很大但不经常变或者不需要实时更新,即使延迟一会也无妨的页面。我个人感觉文件缓存是PHP模板引擎的最重要的部分。
接下来我们就写一个简易的模板引擎(最后有完整文件代码)
首先我们先要计划好我们的所需要的基础类,有Template类和Compile类。
在我们具体实现编译功能之前先搭好一个空的骨架,具体如下:
<?php /** * 模板引擎基础类 */class Template { private$config = array( ‘suffix‘ => ‘.m‘, // 设置模板文件的后缀 ‘templateDir‘ => ‘template/‘, // 设置模板所在的文件夹 ‘compileDir‘ => ‘cache/‘, // 设置编译后存放的目录 ‘cache_html‘ => true, // 是否需要编译成静态的HTML文件 ‘suffix_cache‘ => ‘.html‘, // 设置编译文件的后缀 ‘cache_time‘ => 7200, // 多长时间自动更新,单位秒 ‘php_turn‘ => true, // 是否支持原生PHP代码 ‘cache_control‘ => ‘control.dat‘, ‘debug‘ => false, ); privatestatic$instance = null; private$value = array(); // 值栈private$compileTool; // 编译器public$file; // 模板文件名,不带路径public$debug = array(); // 调试信息private$controlData = array(); publicfunction __construct($config = array()) { $this->debug[‘begin‘] = microtime(true); $this->config = $config + $this->config; if (! is_dir($this->config[‘templateDir‘])) { exit("模板目录不存在!"); } if (! is_dir($this->config[‘compileDir‘])) { mkdir($this->config[‘compileDir‘], 0770); } $this->getPath(); include ‘./Compile.php‘; } /** *获取绝对路径 */publicfunction getPath() { $this->config[‘templateDir‘] = strtr(realpath($this->config[‘templateDir‘]), ‘\\‘, ‘/‘).‘/‘; $this->config[‘compileDir‘] = strtr(realpath($this->config[‘compileDir‘]), ‘\\‘, ‘/‘).‘/‘; } /** *取得模板引擎的实例 */publicstaticfunction getInstance() { if (is_null(self::$instance)) { self::$instance = new self(); } return self::$instance; } /* 设置模板引擎参数 */publicfunction setConfig($key, $value = null) { if (is_array($key)) { $this->config = $key + $this->config; }else { $this->config[$key] = $value; } } /* 获取当前模板引擎配置,仅供调试使用 */publicfunction getConfig($key = null) { if ($key) { return$this->config[$key]; }else { return$this->config; } } /** *注入单个变量 */publicfunction assign($key, $value) { $this->value[$key] = $value; } /** *注入数组变量 */publicfunction assignArray($array) { if (is_array($array)) { foreach($arrayas$k => $v) { $this->value[$k] = $v; } } } /** * 获取模板文件完整路径 */publicfunction path() { return$this->config[‘templateDir‘].$this->file.$this->config[‘suffix‘]; } /** *判断是否开启了缓存 */publicfunction needCache() { return$this->config[‘cache_html‘]; } /** *显示模板 */publicfunction show($file) { } } ?>
从上边的代码中我们能发现对于模板文件不存在和编译文件不存在处理方式不同,这也很容易理解,如果你连模板文件都没有有啥好编译的,但是你有模板文件没编译文件这也很正常,正常进行编译即可。
如上所示,我们首先想好了这个模板引擎需要什么配置,还有一些设置配置的方法和检查配置的方法等,而我们的核心方法show()还没有实现呢,先不着急,我们先去写编译类Compile,如下所示:
<?php class Compile { private$template; // 待编译的文件private$content; // 需要替换的文件private$comfile; // 编译后的文件private$left = ‘{‘; // 左定界符private$right = ‘}‘; // 右定界符private$value = array(); // 值栈private$phpTurn; private$T_P = array(); // 匹配正则数组private$T_R = array(); // 替换数组publicfunction __construct($template, $compileFile, $config) { $this->template = $template; $this->comfile = $compileFile; $this->content = file_get_contents($template); } publicfunction compile() { $this->c_var(); file_put_contents($this->comfile, $this->content); } publicfunction c_var() { $this->content = preg_replace($this->T_P, $this->T_R, $this->content); } publicfunction __set($name, $value) { $this->$name = $value; } publicfunction __get($name) { return$this->$name; } } ?>
从上面Compile类的构造函数我们可以看出,他需要模板文件路径,编译文件路径,和具体编译时的配置参数,但是在这这个配置参数吗,没有用到。之前说过模板引擎主要使用的正则表达式来进行匹配替换,将模板文件编译成能正确执行的PHP文件,这里使用数组存放正则匹配数组和替换数组来进行整体替换。
接下来我们就简单实现几个常用的标签,先看怎么替换简单变量:
// \x7f-\xff表示ASCII字符从127到255,其中\为转义,作用是匹配汉字 $this->T_P[] = "#\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}#"; $this->T_R[] = "<?php echo \$this->value[‘\\1‘]; ?>";
正如我们看到的,上边的那个是正则匹配,下边的是替换。但是我们没有给编译类的value变量赋值,那这个替换能找到正确的值吗?答案是能,因为他用的不是这个类的value用的是模板类的value,接下来一会会讲到。
然后我们在看看怎么实现foreach标签,这个很常用
$this->T_P[] = "#\{(loop|foreach)\s+\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\s*\}#i"; $this->T_P[] = "#\{\/(loop|foreach)\}#";
$this->T_R[] = "<?php foreach ((array)\$this->value[‘\\2‘] as \$k => \$v) { ?>"; $this->T_R[] = "<?php } ?>";
这里用到的主要正则表达式的知识有:元组、反向引用等,这些只要稍微看一下正则表达式基础就能理解了。
我们再来一个if else标签:
$this->T_P[] = "#\{\/(loop|foreach|if)\}#"; $this->T_P[] = "#\{if (.*?)\}#"; $this->T_P[] = "#\{(else if|elseif) (.*?)\}#"; $this->T_P[] = "#\{else\}#"; $this->T_R[] = "<?php } ?>"; $this->T_R[] = "<?php if(\\1){ ?>"; $this->T_R[] = "<?php }elseif(\\2){ ?>"; $this->T_R[] = "<?php }else{ ?>";
我们将if的闭合标签和foreach的闭合标签放一块了。
现在我们已经能编译一些标签了我们就再转回模板类,现在我们想一想要怎么展示呢,这个才是我们的根本目的。写代码之前先理一下思路:
1、判断是否开启了缓存,如果是进行第二步,否则直接进行编译输出。
2、判断是否需要更新缓存(判断方式主要是缓存时间和编译文件和模板文件的修改时间的关系),如果是就进行第三步,否则直接读取缓存文件输出。
3、重新编译模板文件,并将编译后的PHP文件输出保存到静态缓存文件中。
简单来说就是上边的那三个步骤,具体实现如下:
/* * *是否需要重新生成静态文件 */ public function reCache($file) { $flag = true; $cacheFile = $this->config[‘compileDir‘].md5($file).$this->config[‘suffix_cache‘]; if ($this->config[‘cache_html‘] === true) { $timeFlag = (time() - @filemtime($cacheFile)) < $this->config[‘cache_time‘] ? true : false; if (is_file($cacheFile) && filesize($cacheFile) > 1 && $timeFlag && filemtime($compileFile) >= filemtime($this->path())) { $flag = false; }else { $flag = true; } } return$flag; } /** *显示模板 */publicfunction show($file) { $this->file = $file; if (! is_file($this->path())) { exit(‘找不到对应的模板!‘); } $compileFile = $this->config[‘compileDir‘].md5($file).‘.php‘; $cacheFile = $this->config[‘compileDir‘].md5($file).$this->config[‘suffix_cache‘]; extract($this->value, EXTR_OVERWRITE); if ($this->config[‘cache_html‘] === true) { if ($this->reCache($file) === true) { $this->debug[‘cached‘] = ‘false‘; $this->compileTool = new Compile($this->path(), $compileFile, $this->config); if ($this->needCache()) {ob_start();} if (! is_file($compileFile) || filemtime($compileFile) < filemtime($this->path())) { $this->compileTool->value = $this->value; $this->compileTool->compile(); include$compileFile; }else { include$compileFile; } if ($this->needCache()) { $message = ob_get_contents(); file_put_contents($cacheFile, $message); } }else { readfile($cacheFile); $this->debug[‘cached‘] = ‘true‘; } }else { if (! is_file($compileFile) || filemtime($compileFile) < filemtime($this->path())) { $this->compileTool = new Compile($this->path(), $compileFile, $this->config); $this->compileTool->value = $this->value; $this->compileTool->compile(); include$compileFile; }else { include$compileFile; } } $this->debug[‘spend‘] = microtime(true) - $this->debug[‘begin‘]; $this->debug[‘count‘] = count($this->value); }
上边的代码基本是按照上述的三个步骤来进行的,好好看一下不难理解。
接下来就是模板文件了:
<html> {$data},{$person} <br/>列表一:<br/> <ul> {foreach$arr1} <li>$v</li> {/foreach} </ul> <br/>列表二:<br/> <ul> {loop $arr2} <li>$v</li> {/loop} </ul> {if$a == ‘1‘}a等于1 {elseif$a == ‘2‘}a等于2 {else}a不等于1也不等于2 {/if} </html>
这个模板文件主要测试了之前我们事先的模板标签。
下面写一个测试文件:
<?php include_once ‘./Template.php‘; $tpl = new Template(); $tpl->assign(‘data‘, ‘hello‘); $tpl->assign(‘person‘, ‘world!‘); $tpl->assign(‘arr1‘, array(‘123‘,‘456‘,‘789‘)); $tpl->assign(‘arr2‘, array(‘abc‘, ‘def‘, ‘ghi‘)); $tpl->assign(‘a‘, ‘2‘); $tpl->show(‘member‘); ?>
这就是一个简单的测试我们的模板引擎能不能用的测试用例。
下面我们看看完整代码吧:
<?php /** * 模板引擎基础类 */class Template { private$config = array( ‘suffix‘ => ‘.m‘, // 设置模板文件的后缀 ‘templateDir‘ => ‘template/‘, // 设置模板所在的文件夹 ‘compileDir‘ => ‘cache/‘, // 设置编译后存放的目录 ‘cache_html‘ => true, // 是否需要编译成静态的HTML文件 ‘suffix_cache‘ => ‘.html‘, // 设置编译文件的后缀 ‘cache_time‘ => 7200, // 多长时间自动更新,单位秒 ‘php_turn‘ => true, // 是否支持原生PHP代码 ‘cache_control‘ => ‘control.dat‘, ‘debug‘ => false, ); privatestatic$instance = null; private$value = array(); // 值栈private$compileTool; // 编译器public$file; // 模板文件名,不带路径public$debug = array(); // 调试信息private$controlData = array(); publicfunction __construct($config = array()) { $this->debug[‘begin‘] = microtime(true); $this->config = $config + $this->config; if (! is_dir($this->config[‘templateDir‘])) { exit("模板目录不存在!"); } if (! is_dir($this->config[‘compileDir‘])) { mkdir($this->config[‘compileDir‘], 0770); } $this->getPath(); include ‘./Compile.php‘; } /** *获取绝对路径 */publicfunction getPath() { $this->config[‘templateDir‘] = strtr(realpath($this->config[‘templateDir‘]), ‘\\‘, ‘/‘).‘/‘; $this->config[‘compileDir‘] = strtr(realpath($this->config[‘compileDir‘]), ‘\\‘, ‘/‘).‘/‘; } /** *取得模板引擎的实例 */publicstaticfunction getInstance() { if (is_null(self::$instance)) { self::$instance = new self(); } return self::$instance; } /* 设置模板引擎参数 */publicfunction setConfig($key, $value = null) { if (is_array($key)) { $this->config = $key + $this->config; }else { $this->config[$key] = $value; } } /* 获取当前模板引擎配置,仅供调试使用 */publicfunction getConfig($key = null) { if ($key) { return$this->config[$key]; }else { return$this->config; } } /** *注入单个变量 */publicfunction assign($key, $value) { $this->value[$key] = $value; } /** *注入数组变量 */publicfunction assignArray($array) { if (is_array($array)) { foreach($arrayas$k => $v) { $this->value[$k] = $v; } } } /** * 获取模板文件完整路径 */publicfunction path() { return$this->config[‘templateDir‘].$this->file.$this->config[‘suffix‘]; } /** *判断是否开启了缓存 */publicfunction needCache() { return$this->config[‘cache_html‘]; } /** *是否需要重新生成静态文件 */publicfunction reCache($file) { $flag = true; $cacheFile = $this->config[‘compileDir‘].md5($file).$this->config[‘suffix_cache‘]; if ($this->config[‘cache_html‘] === true) { $timeFlag = (time() - @filemtime($cacheFile)) < $this->config[‘cache_time‘] ? true : false; if (is_file($cacheFile) && filesize($cacheFile) > 1 && $timeFlag && filemtime($compileFile) >= filemtime($this->path())) { $flag = false; }else { $flag = true; } } return$flag; } /** *显示模板 */publicfunction show($file) { $this->file = $file; if (! is_file($this->path())) { exit(‘找不到对应的模板!‘); } $compileFile = $this->config[‘compileDir‘].md5($file).‘.php‘; $cacheFile = $this->config[‘compileDir‘].md5($file).$this->config[‘suffix_cache‘]; extract($this->value, EXTR_OVERWRITE); if ($this->config[‘cache_html‘] === true) { // 开启缓存的处理逻辑if ($this->reCache($file) === true) { // 需要更新缓存的处理逻辑$this->debug[‘cached‘] = ‘false‘; $this->compileTool = new Compile($this->path(), $compileFile, $this->config); if ($this->needCache()) {ob_start();} // 打开输出控制缓冲if (! is_file($compileFile) || filemtime($compileFile) < filemtime($this->path())) { $this->compileTool->value = $this->value; $this->compileTool->compile(); include$compileFile; }else { include$compileFile; } if ($this->needCache()) { $message = ob_get_contents(); // 获取输出缓冲中的内容(在include编译文件的时候就有输出了)file_put_contents($cacheFile, $message); } }else { readfile($cacheFile); $this->debug[‘cached‘] = ‘true‘; } }else { if (! is_file($compileFile) || filemtime($compileFile) < filemtime($this->path())) { $this->compileTool = new Compile($this->path(), $compileFile, $this->config); $this->compileTool->value = $this->value; $this->compileTool->compile(); include$compileFile; }else { include$compileFile; } } $this->debug[‘spend‘] = microtime(true) - $this->debug[‘begin‘]; $this->debug[‘count‘] = count($this->value); //$this->debug_info(); } publicfunction debug_info() { if ($this->config[‘debug‘] === true) { echoPHP_EOL, ‘---------debug info---------‘, PHP_EOL; echo "程序运行日期:", date("Y-m-d H:i:s"), PHP_EOL; echo "模板解析耗时:", $this->debug[‘spend‘], ‘秒‘, PHP_EOL; echo ‘模板包含标签数目:‘, $this->debug[‘count‘], PHP_EOL; echo ‘是否使用静态缓存:‘, $this->debug[‘cached‘], PHP_EOL; echo ‘模板引擎实例参数:‘, var_dump($this->getConfig()); } } /** *清理缓存的HTML文件 */publicfunction clean($path = null) { if ($path === null) { $path = $this->config[‘compileDir‘]; $path = glob($path.‘* ‘.$this->config[‘suffix_cache‘]); }else { $path = $this->config[‘compileDir‘].md5($path).$this->config[‘suffix_cache‘]; } foreach((array)$pathas$v) { unlink($v); } } } ?>
<?php class Compile { private$template; // 待编译的文件private$content; // 需要替换的文件private$comfile; // 编译后的文件private$left = ‘{‘; // 左定界符private$right = ‘}‘; // 右定界符private$value = array(); // 值栈private$phpTurn; private$T_P = array(); // 匹配正则数组private$T_R = array(); // 替换数组publicfunction __construct($template, $compileFile, $config) { $this->template = $template; $this->comfile = $compileFile; $this->content = file_get_contents($template); if ($config[‘php_turn‘] === true) { $this->T_P[] = "#<\?(=|php|)(.+?)\?#is"; $this->T_R[] = "<?\1\2?>"; } // 变量匹配 // \x7f-\xff表示ASCII字符从127到255,其中\为转义,作用是匹配汉字$this->T_P[] = "#\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}#"; // foreach标签盘匹配$this->T_P[] = "#\{(loop|foreach)\s+\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\s*\}#i"; $this->T_P[] = "#\{\/(loop|foreach|if)\}#"; $this->T_P[] = "#\{([k|v])\}#"; // if else标签匹配$this->T_P[] = "#\{if (.*?)\}#"; $this->T_P[] = "#\{(else if|elseif) (.*?)\}#"; $this->T_P[] = "#\{else\}#"; $this->T_P[] = "#\{(\#|\*)(.*?)(\#|\*)\}#"; $this->T_R[] = "<?php echo \$this->value[‘\\1‘]; ?>"; $this->T_R[] = "<?php foreach ((array)\$this->value[‘\\2‘] as \$k => \$v) { ?>"; $this->T_R[] = "<?php } ?>"; $this->T_R[] = "<?php echo \$\\1?>"; $this->T_R[] = "<?php if(\\1){ ?>"; $this->T_R[] = "<?php }elseif(\\2){ ?>"; $this->T_R[] = "<?php }else{ ?>"; $this->T_R[] = ""; } publicfunction compile() { $this->c_var(); //$this->c_staticFile();file_put_contents($this->comfile, $this->content); } publicfunction c_var() { $this->content = preg_replace($this->T_P, $this->T_R, $this->content); } /* 对引入的静态文件进行解析,应对浏览器缓存 */publicfunction c_staticFile() { $this->content = preg_replace(‘#\{\!(.*?)\!\}#‘, ‘<script src=\1‘.‘?t=‘.time().‘></script>‘, $this->content); } publicfunction __set($name, $value) { $this->$name = $value; } publicfunction __get($name) { return$this->$name; } } ?>
模板文件member.m代码:
<html> {$data},{$person} <br/>列表一:<br/> <ul> {foreach$arr1} <li>$v</li> {/foreach} </ul> <br/>列表二:<br/> <ul> {loop $arr2} <li>$v</li> {/loop} </ul> {if$a == ‘1‘}a等于1 {elseif$a == ‘2‘}a等于2 {else}a不等于1也不等于2 {/if} </html>
测试用例:
<?php include_once ‘./Template.php‘; $tpl = new Template(); $tpl->assign(‘data‘, ‘hello‘); $tpl->assign(‘person‘, ‘world!‘); $tpl->assign(‘arr1‘, array(‘123‘,‘456‘,‘789‘)); $tpl->assign(‘arr2‘, array(‘abc‘, ‘def‘, ‘ghi‘)); $tpl->assign(‘a‘, ‘2‘); $tpl->show(‘member‘); ?>
本文内容大部分来自于《PHP核心技术与最佳实践》的第六章。
原文:https://www.cnblogs.com/wp1996/p/9011892.html
内容总结
以上是互联网集市为您收集整理的一个简单的PHP模板引擎全部内容,希望文章能够帮你解决一个简单的PHP模板引擎所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。