博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个编译型的PHP模板引擎大致实现过程
阅读量:6876 次
发布时间:2019-06-26

本文共 3230 字,大约阅读时间需要 10 分钟。

  hot3.png

没啥解说。。直接代码:

 JTemplate.class.php

templateDir = $templateDir; $this->templateCompileDir = $templateComplieDir; } /** * 显示模板 * @param string $fileName 模板文件名 */ public function display($fileName){ $this->fileName = $fileName; if(file_exists($this->templateDir.'/'.$this->fileName)){ $compileFileName = $this->templateCompileDir.'/'.$this->file_safe_name().'.php'; if(!file_exists($compileFileName) || filemtime($compileFileName)< filemtime($this->templateDir.'/'.$this->fileName)){ $this->del_old_file(); $this->compile(); } extract($this->templateVar); include $compileFileName; }else{ $this->error('Sorry,the template file '.$this->fileName.' does not exist!!'); } } /** * 获取编译文件名 */ private function get_compile_file(){ $compileFile = explode('.',$this->fileName); unset($compileFile[count($compileFile)-1]); return implode('.',$compileFile); } /** * 编译 */ private function compile(){ $fileHandle = @fopen($this->templateDir.'/'.$this->fileName, 'r'); while(!feof($fileHandle)){ $fileContent = fread($fileHandle,1024); } fclose($fileHandle); $fileContent = $this->template_replace($fileContent); //$compileFile = $this->get_compile_file($fileName); $fileHandle = @fopen($this->templateCompileDir.'/'.$this->file_safe_name().'.php','w'); if($fileHandle){ fwrite($fileHandle, $fileContent); fclose($fileHandle); }else{ $this->error('Sorry,Compile dir can not write!'); } } /** * 模板传值 * @param string $valueName 模板中使用的变量名 * @param $value 变量值 */ public function assign($valueName,$value){ $this->templateVar[$valueName] = $value; } /** * 模板正则替换 * @param string $content 替换内容 * string 替换过后的内容 */ private function template_replace($content){ $orginArray = array( '/
/s', '/
/s', '/
(.+?)
/s', '/
/s', '/
/s', '/
/s', '/
/s', '/
/s', '/\{P:(.+?)\:}/s', '/\{C:(\w+)\}/s', '/\{I:(.+?)\}/s', '/\{F:(.+?)\}/s', '/\{EF:(.+?)\}/s', '/\{([a-zA-Z0-9_\[\]\'\"\$\.\x7f-\xff]+)\}/s', ); $changeArray = array( '
', '
$$3){$countLoop++;?>', '
$1
', '
', '
', '
', '
', '
', '
', '
', '
templateDir.'/$1";?>', '
', '
', '
', ); return $repContent=preg_replace($orginArray,$changeArray,$content); } /** * 删除旧文件 */ private function del_old_file(){ $compileFile = $this->get_compile_file($this->fileName); $files = glob($this->templateCompileDir.'/'.$compileFile.'*.php'); // print_r($files); if($files){ @unlink($files[0]); } } /** * 编译文件名安全处理方法 * string 返回编译文件名 */ private function file_safe_name(){ $compileFile = $this->get_compile_file($this->fileName); return $compileFile.filemtime($this->templateDir.'/'.$this->fileName); } /** * 错误输出函数 * @param string $content 错误输出信息 */ private function error($content){ $stringHtml = '
'; $stringHtml .= 'Error information:
'; $stringHtml .= '
'; $stringHtml .= $content; $stringHtml .= ''; $stringHtml .= '
'; exit($stringHtml); }}?>

index.php

assign('a', $a);$template->display('index.html');?>

 

转载于:https://my.oschina.net/cdark/blog/64790

你可能感兴趣的文章
你如何理解HTML结构的语义化?
查看>>
JQuery Ajax 的简单使用
查看>>
Codeforces Round #287 (Div. 2) ABCDE
查看>>
【转载】读懂IL代码就这么简单(二)
查看>>
09-JS的事件流的概念(重点)
查看>>
有关inline-block
查看>>
文献随笔(九)
查看>>
git相关
查看>>
加入大型的js文件如jQuery文件,Eclipse会报错
查看>>
POJ 2763 (树链剖分+边修改+边查询)
查看>>
全局变量---只创建一次
查看>>
IOS APP上下黑边问题
查看>>
数位dp题集
查看>>
C# 汉字转拼音
查看>>
jquery实现复制的两种方式
查看>>
Django分页(一)
查看>>
Balance Adjustment页面调整无法保存的问题
查看>>
De Moivre–Laplace theorem
查看>>
symfony2使用form指定的checkbox,设置其属性disabled
查看>>
linux操作之软件安装(一)
查看>>