首页 > 编程 > PHP > 正文

[李景山php]thinkphp核心源码注释-View.class.php

2020-03-22 17:08:43
字体:
来源:转载
供稿:网友
  • <?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]// +----------------------------------------------------------------------// | Copyright (c) 2006-2014 http://thinkVeVb.com All rights reserved.// +----------------------------------------------------------------------// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )// +----------------------------------------------------------------------// | Author: liu21st <liu21st@gmail.com>// +----------------------------------------------------------------------namespace Think;/** * ThinkPHP 视图类 * 有点视频抽象类的概念, 高层,应用层的感觉,哈哈 */html' target='_blank'>class View {    /**     * 模板输出变量     * @var tVar     * @access protected     */     protected $tVar     =   array(); // 模版变量    /**     * 模板主题     * @var theme     * @access protected     */     protected $theme    =   ''; // 居然还有主题,哈哈    /**     * 模板变量赋值     * @access public     * @param mixed $name     * @param mixed $value     */    public function assign($name,$value=''){        if(is_array($name)) {            $this->tVar   =  array_merge($this->tVar,$name);        }else {            $this->tVar[$name] = $value;        }    }//  这里的赋值 太简单了,以前觉得很神奇的产品,呵呵    /**     * 取得模板变量的值     * @access public     * @param string $name     * @return mixed     */    public function get($name=''){        if('' === $name) {            return $this->tVar;        }        return isset($this->tVar[$name])?$this->tVar[$name]:false;    }    // 这里的 get 方式 这里还真的没用过,不过此处第一个看见了,不错 同样的方式了    /**     * 加载模板和页面输出 可以返回输出内容     * @access public     * @param string $templateFile 模板文件名     * @param string $charset 模板输出字符集     * @param string $contentType 输出类型     * @param string $content 模板输出内容     * @param string $prefix 模板缓存前缀     * @return mixed     */    public function display($templateFile='',$charset='',$contentType='',$content='',$prefix='') {        G('viewStartTime'); // 记录开始        // 视图开始标签        Hook::listen('view_begin',$templateFile);// 你是要干啥呢, 一般情况下为空        // view_begin 其实就是个 函数了  实际中,貌似没有这个函数呢 ,也就是可以自己拓展的东西        // 没什么用啦        // 解析并获取模板内容        $content = $this->fetch($templateFile,$content,$prefix);// 这里的 获取模版信息        // 解析出文件的大小        // 输出模板内容        $this->render($content,$charset,$contentType); // 模版输出,        // 直接echo 输出啊,大哥你牛叉呢        // 视图结束标签        Hook::listen('view_end');        // view_end 其实就是个 函数了        // 拓展, 终于发现了 Hook:: listen 的用法,    } // 这个外包太强悍了    /**     * 输出内容文本可以包括Html     * @access private     * @param string $content 输出内容     * @param string $charset 模板输出字符集     * @param string $contentType 输出类型     * @return mixed     */    private function render($content,$charset='',$contentType=''){        if(empty($charset))  $charset = C('DEFAULT_CHARSET');        if(empty($contentType)) $contentType = C('TMPL_CONTENT_TYPE');        // 网页字符编码        header('Content-Type:'.$contentType.'; charset='.$charset);        header('Cache-control: '.C('HTTP_CACHE_CONTROL'));  // 页面缓存控制        header('X-Powered-By:ThinkPHP');        // 输出模板文件        echo $content;    }// 直接输出 html 文件呢, 看来不是包含进来吗    /**     * 解析和获取模板内容 用于输出     * @access public     * @param string $templateFile 模板文件名     * @param string $content 模板输出内容     * @param string $prefix 模板缓存前缀     * @return string     */    public function fetch($templateFile='',$content='',$prefix='') {        if(empty($content)) { // 其实用的最多的,就是都是 空            $templateFile   =   $this->parseTemplate($templateFile); // 返回模版文件            // 模板文件不存在直接返回            if(!is_file($templateFile)) E(L('_TEMPLATE_NOT_EXIST_').':'.$templateFile);        }else{            defined('THEME_PATH') or    define('THEME_PATH', $this->getThemePath());        } // 找到 主题 位置        // 页面缓存        ob_start();        ob_implicit_flush(0); // PHP ob_implicit_flush 打开/关闭绝对刷送        // ob_implicit_flush()将打开或关闭绝对(隐式)刷送。绝对(隐式)刷送将导致在每次输出调用后有一次刷送操作,以便不再需要对 flush() 的显式调用。        // 设为TRUE 打开绝对刷送,反之是 FALSE 。        // 简单点说,就是 关闭了缓存的直接输出,讲内容先输出到缓存区        if('php' == strtolower(C('TMPL_ENGINE_TYPE'))) { // 使用PHP原生模板            //  'TMPL_ENGINE_TYPE'      =>  'Think',     // 默认模板引擎 以下设置仅对使用Think模板引擎有效            $_content   =   $content;            // 模板阵列变量分解成为独立变量            extract($this->tVar, EXTR_OVERWRITE);            // 直接载入PHP模板            empty($_content)?include $templateFile:eval('?>'.$_content);        }else{            // 视图解析标签            $params = array('var'=>$this->tVar,'file'=>$templateFile,'content'=>$content,'prefix'=>$prefix);            Hook::listen('view_parse',$params); // 这里没什么了,好像没这个函数了        }        // 获取并清空缓存        $content = ob_get_clean();        // 内容过滤标签        Hook::listen('view_filter',$content); // 貌似,也没什么鸟用        // 输出模板文件        return $content; // 把数据进行了返回  我感觉是啥也没有呢  content 其实不是啥也没有吗    } // 这里的位置    /**     * 自动定位模板文件     * @access protected     * @param string $template 模板文件规则     * @return string     */    public function parseTemplate($template='') {        if(is_file($template)) {            return $template;        } // 有的话,没有了        $depr       =   C('TMPL_FILE_DEPR');        // 'TMPL_FILE_DEPR'        =>  '/', //模板文件CONTROLLER_NAME与ACTION_NAME之间的分割符        $template   =   str_replace(':', $depr, $template);        // 获取当前模块        $module   =  MODULE_NAME;        if(strpos($template,'@')){ // 跨模块调用模版文件            list($module,$template)  =   explode('@',$template);        }        // 获取当前主题的模版路径        defined('THEME_PATH') or    define('THEME_PATH', $this->getThemePath($module));        // 分析模板文件规则        if('' == $template) {            // 如果模板文件名为空 按照默认规则定位            $template = CONTROLLER_NAME . $depr . ACTION_NAME;        }elseif(false === strpos($template, $depr)){            $template = CONTROLLER_NAME . $depr . $template;        }        $file   =   THEME_PATH.$template.C('TMPL_TEMPLATE_SUFFIX');        if(C('TMPL_LOAD_DEFAULTTHEME') && THEME_NAME != C('DEFAULT_THEME') && !is_file($file)){            // 找不到当前主题模板的时候定位默认主题中的模板            $file   =   dirname(THEME_PATH).'/'.C('DEFAULT_THEME').'/'.$template.C('TMPL_TEMPLATE_SUFFIX');        }        return $file; // 这里,默认是返回文件名称    }    /**     * 获取当前的模板路径     * @access protected     * @param  string $module 模块名     * @return string     */    protected function getThemePath($module=MODULE_NAME){        // 获取当前主题名称        $theme = $this->getTemplateTheme();        // 获取当前主题的模版路径        $tmplPath   =   C('VIEW_PATH'); // 模块设置独立的视图目录        if(!$tmplPath){             // 定义TMPL_PATH 则改变全局的视图目录到模块之外            $tmplPath   =   defined('TMPL_PATH')? TMPL_PATH.$module.'/' : APP_PATH.$module.'/'.C('DEFAULT_V_LAYER').'/';        }        return $tmplPath.$theme;    } // 返回对应主体的路径    /**     * 设置当前输出的模板主题     * @access public     * @param  mixed $theme 主题名称     * @return View     */    public function theme($theme){        $this->theme = $theme;        return $this;    }// 返回主题    /**     * 获取当前的模板主题     * @access private     * @return string     */    private function getTemplateTheme() {        if($this->theme) { // 指定模板主题            $theme = $this->theme;        }else{            /* 获取模板主题名称 */            $theme =  C('DEFAULT_THEME');            if(C('TMPL_DETECT_THEME')) {// 自动侦测模板主题                $t = C('VAR_TEMPLATE');                if (isset($_GET[$t])){                    $theme = $_GET[$t];                }elseif(cookie('think_template')){                    $theme = cookie('think_template');                }                if(!in_array($theme,explode(',',C('THEME_LIST')))){                    $theme =  C('DEFAULT_THEME');                }                cookie('think_template',$theme,864000);            }        }        defined('THEME_NAME') || define('THEME_NAME',   $theme);                  // 当前模板主题名称        return $theme?$theme . '/':'';    }// 都是关于主题位置的一个获取了}// 总结:
    PHP编程

    郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表