mPHP核心框架——视图层的代码实现

[文章作者:磨延城 转载请注明原文出处: https://mo2g.com/view/98/ ]

视图层的作用是,把复杂的PHP代码跟html代码尽可能的分离开,让前端开发人员跟后端开发人员协同工作,最后在通过模版机制,把最终的静态html效果显示出来.

视图层的作用是,把复杂的PHP代码跟html代码尽可能的分离开,让前端开发人员跟后端开发人员协同工作,最后通过视图的模版机制,把变量跟模版代码编译到一起,最后静态html效果就能显示出来。

/*
作者:moyancheng
创建时间:2012-03-01
最后更新时间:2014-01-11
*/
class view {
    //加载xxx.tpl.html模版文件
    //$tpl:模版文件
    //$file:根据模版生成的静态文件
    public function loadTpl($tpl,$file = '') {
        $arrData = $this->_include($tpl,$file);
        $date = date('Y-m-d H:i');
        $arrData['html'] .= "<!-- mPHP html cache $date -->";
        if( ($strDir = dirname($arrData['file']) ) && !is_dir($strDir) ) mkdir($strDir,0775,true);
        file_put_contents($arrData['file'],$arrData['html']);
    }
    
    //编译模版
    public function tplCompile($str) {
        global $CFG;
        //处理include标签
        $str = preg_replace( "/<!--#\s*layout\s*:\s*([^ ]+);*\s*#-->/", '<?php $this->_include(\'\\1\') ?>', $str );
        /*替换<!--# #-->标签为<?php ?>*/
        $str = strtr($str,array($CFG['template']['tag_left'] => '<?php ', $CFG['template']['tag_right'] => ' ?>'));
        unset($CFG);
        return $str;
    }
    
    public function _include($tpl, $file = '') {
        global $CFG;
        $tpl_file = TPL_PATH."{$tpl}.tpl.html";
        $tpl_c_file = TPL_C_PATH . "{$tpl}.tpl.php";
        
        //判断是否需要重新编译模版
        if( filemtime($tpl_file) != filemtime($tpl_c_file) ) {
            //tpl模版 转译 php文件 并保存
            $html = file_get_contents($tpl_file);
            $html = $this->tplCompile($html);//替换标签
            file_put_contents($tpl_c_file,$html);
            touch($tpl_c_file,filemtime($tpl_file));//编译文件与模版文件同步修改时间
        }
        
        //php文件 转译 静态html
        ob_start();
        include $tpl_c_file;
        $html = ob_get_clean();
        
        //保存静态html
        if(!$CFG['debug']) $html = mini_html($html);
        unset($CFG);
        echo $html;
        
        if($file == '') $file = CACHE_HTML_PATH . "{$tpl}.html";
        $arrData['file'] = $file;
        $arrData['html'] = $html;
        return $arrData;
    }
    
    public function cache($file,$cacheTime) {
        global $CFG;
        $time = $_SERVER['REQUEST_TIME'];
       
        if( (is_file($file) && (filemtime($file) + $cacheTime >= $time) ) && !$CFG['debug'] ) {
            unset($CFG);
            include $file;exit;
            return true;
        } else {
            unset($CFG);
        }
        return false;
    }
}

这里需要定义两个常量:

1:TPL_PATH,模版存储位置

2:TPL_C_PATH,编译好的模版存储位置

假设两个常量设置为同一目录,接下来,就用个简单的例子体验视图层的使用:

在根目录下创建a.tpl.html文件,内容如下

当前测试模版文件名为a.tpl.html<br>

在根目录下创建index.tpl.html文件,内容如下

<html>
<head></head>
<body>
当前测试模版文件名为index.tpl.html<br>
<!--# layout:a #-->
时间戳=<!--# echo $this->data['time']#-->
</body>
</html>
class indexController extends controller {
    public function __construct() {}
    
    public function indexAction() {
        $tpl = 'index';
        $file = 'index.html';
        self::$view->cache($file,60);//缓存60秒
        self::$view->data['time'] = time();
        self::$view->loadTpl($tpl,$file);
    }
}

通过访问http://localhost/xxx/?c=index&a=index的时候,显示效果类似于

当前测试模版文件名为index.tpl.html

当前测试模版文件名为a.tpl.html

时间戳=1396865471

现在让我们修改一下模版index.tpl.html,把“当前测试模版文件名为”改成“当前测试模版文件名=”,再刷新页面看看效果。

这个时候,你会发现,页面内容居然没变化,这是因为self::$view->cache($file,60);的缓存功能还没有失效,如果想立马看到效果,可以把60改为0,然后再刷新一下页面。这下终于出现了?

评论:

  1. 暂无评论...
  2. 我来说两句:

      切换  

    磨途歌检测发现,您当前使用的浏览器版本过低,要想使用画板模式,请先更新浏览器

      切换  

    磨途歌随机验证码