首页 > php教程 > PHP源码 > 正文

编译型PHP模板引擎大致实现过程

PHP中文网
发布: 2016-05-25 17:10:49
原创
1491人浏览过

模板引擎

ThinkPHP5.0完整版
ThinkPHP5.0完整版

ThinkPHP5.0版本是一个颠覆和重构版本,官方团队历时十月,倾注了大量的时间和精力,采用全新的架构思想,引入了更多的PHP新特性,优化了核心,减少了依赖,实现了真正的惰性加载,支持composer,并针对API开发做了大量的优化,包括路由、日志、异常、模型、数据库、模板引擎和验证等模块都已经重构,不适合原有3.2项目的升级,请慎重考虑商业项目升级,但绝对是新项目的首选(无论是WEB还是API

ThinkPHP5.0完整版 2228
查看详情 ThinkPHP5.0完整版
<?php
/**
 * @author Jiawei
 * @Completed in 2012-6-29 0:23
 */
class JTemplate{
    //通过assign函数传入的变量临时存放数组
    private $templateVar = array();
    //模板目录
    private $templateDir = '';
    //编译目录
    private $templateCompileDir = '';
     
    private $fileName = '';
    /**
     * 构造函数
     * @param string $templateDir 模板目录
     * @param string $templateComplieDir 模板编译目录
     */
    public function __construct($templateDir,$templateComplieDir){
        $this->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 替换内容
     * @return string 替换过后的内容
     */
    private function template_replace($content){
        $orginArray = array(
            '/<!--loop\s+\$(\w+)\s+\$(\w+)-->/s',
            '/<!--loop\s+\$(\w+)\s+\$(\w+)\s+\$(\w+)-->/s',
            '/<!--elseloop-->(.+?)<!--endloop-->/s',
            '/<!--endloop-->/s',
            '/<!--if\s+\((.+?)\)-->/s',
            '/<!--endif-->/s',
            '/<!--elseif\s+\((.+?)\)-->/s',
            '/<!--else-->/s',
            '/\{P:(.+?)\:}/s',
            '/\{C:(\w+)\}/s',
            '/\{I:(.+?)\}/s',
            '/\{F:(.+?)\}/s',
            '/\{EF:(.+?)\}/s',
            '/\{([a-zA-Z0-9_\[\]\'\"\$\.\x7f-\xff]+)\}/s',
        );
         
        $changeArray = array(
            '<?php if(!empty($$1)&&is_array($$1)){$countLoop = 1;foreach($$1 as $$2){$countLoop++;?>',
            '<?php if(!empty($$1)&&is_array($$1)){$countLoop = 1;foreach($$1 as $$2=>$$3){$countLoop++;?>',
            '<?php }if(!empty($countLoop))$countLoop--;}else{?>$1<?php }?>',
            '<?php }if(!empty($countLoop))$countLoop--;}?>',
            '<?php if($1){?>',
            '<?php }?>',
            '<?php }elseif($1){?>',
            '<?php }else{?>',
            '<?php $1?>',
            '<?php echo $1;?>',
            '<?php include_once "'.$this->templateDir.'/$1";?>',
            '<?php $1;?>',
            '<?php echo $1;?>',
            '<?php echo $$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]);
        }
    }
    /**
     * 编译文件名安全处理方法
     * @return 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 = '<div style="width:780px;height:auto;padding:10px;border:1px solid #CCC;margin:0 auto;">';
        $stringHtml .= 'Error information:<br />';
        $stringHtml .= '<font color="red">';
        $stringHtml .= $content;
        $stringHtml .= '</font>';
        $stringHtml .= '</div>';
        exit($stringHtml);
    }
}
?>
登录后复制

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号