CI-View操作源码分析


这章研究CI如何渲染View层。


Tips

输出控制

  • ob_系列。
  • 将数组的键值对转为从数组中将变量导入到当前的符号表。
int extract ( array &$var_array [, int $extract_type = EXTR_OVERWRITE [, string $prefix = NULL ]] )
  • 返回由 obj 指定的对象中定义的属性组成的关联数组。
array get_object_vars ( object $obj ) 
  • 参数:short_open_tag,短标签写法:<?=$xxx;?>

流程

  1. 确定视图文件是否存在,存在则获取文件路径。
  2. 打开输出控制
  3. 根据 嵌套级别决定进行输出还是保存到output的final_output内。(这里实现的很好,赞!)
  4. 最终,所有被缓存的输出将由 Output::_display()统一输出、处理。

源码

$this->load->view('myview', arrar('key' => 'value));

我们可以通过这种方式输出视图,它实际调用了Load.php 定义的全局函数 view()_ci_load() 跟进:

/**
     * View Loader
     *
     * Loads "view" files.
     *
     * @param    string    $view    View name
     * @param    array    $vars    An associative array of data
     *                to be extracted for use in the view
     * @param    bool    $return    Whether to return the view output
     *                or leave it to the Output class
     * @return    object|string
     */
    public function view($view, $vars = array(), $return = FALSE)
    {
        return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
    }


/**
     * Internal CI Data Loader
     *
     * Used to load views and files.
     *
     * Variables are prefixed with _ci_ to avoid symbol collision with
     * variables made available to view files.
     *
     * @used-by    CI_Loader::view()
     * @used-by    CI_Loader::file()
     * @param    array    $_ci_data    Data to load
     * @return    object
     */
    protected function _ci_load($_ci_data)
    {
        // Set the default data variables
        foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
        {
            $$_ci_val = isset($_ci_data[$_ci_val]) ? $_ci_data[$_ci_val] : FALSE;
        }

        $file_exists = FALSE;

        // Set the path to the requested file
        if (is_string($_ci_path) && $_ci_path !== '')
        {
            $_ci_x = explode('/', $_ci_path);
            //将数组的内部指针指向最后一个单元以互殴文件名
            $_ci_file = end($_ci_x);
        }
        else
        {
            $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
            $_ci_file = ($_ci_ext === '') ? $_ci_view.'.php' : $_ci_view;

            //尝试获取视图文件
            foreach ($this->_ci_view_paths as $_ci_view_file => $cascade)
            {
                if (file_exists($_ci_view_file.$_ci_file))
                {
                    $_ci_path = $_ci_view_file.$_ci_file;
                    $file_exists = TRUE;
                    break;
                }

                if ( ! $cascade)
                {
                    break;
                }
            }
        }

        // 请求的文件不存在,报错。
        if ( ! $file_exists && ! file_exists($_ci_path))
        {
            show_error('Unable to load the requested file: '.$_ci_file);
        }


        // 这让我们可以通过 $this->load->key 的方式调用 Controller 包含的变量。
        // This allows anything loaded using $this->load (views, files, etc.)
        // to become accessible from within the Controller and Model functions.
        $_ci_CI =& get_instance();
        foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
        {
            if ( ! isset($this->$_ci_key))
            {
                $this->$_ci_key =& $_ci_CI->$_ci_key;
            }
        }

        /*
         * Extract and cache variables
         *
         * You can either set variables using the dedicated $this->load->vars()
         * function or via the second parameter of this function. We'll merge
         * the two types and cache them so that views that are embedded within
         * other views can have access to these variables.
         */
        if (is_array($_ci_vars))
        {
            $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
        }

        //从数组中将变量导入到当前的符号表
        extract($this->_ci_cached_vars);

        /*
         * Buffer the output
         *
         * We buffer the output for two reasons:
         * 1. Speed. You get a significant speed boost. 显著提高的速度
         * 2. So that the final rendered template can be post-processed by
         *    the output class. Why do we need post processing? For one thing,
         *    in order to show the elapsed page load time. Unless we can
         *    intercept the content right before it's sent to the browser and
         *    then stop the timer it won't be accurate.
         */
        // 打开输出缓冲,之后的输出将不会显示出来
        ob_start();

        // If the PHP installation does not support short tags we'll
        // do a little string replacement, changing the short tags
        // to standard PHP echo statements.
        if ( ! is_php('5.4') && ! ini_get('short_open_tag') && config_item('rewrite_short_tags') === TRUE)
        {
            // 当服务器不支持 short_open_tag 时采用这种曲线救国的方法。
            echo eval('?>'.preg_replace('/;*\s*\?>/', '; ?>', str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
        }
        else
        {
            //包含
            include($_ci_path); // include() vs include_once() allows for multiple views with the same name
        }

        log_message('info', 'File loaded: '.$_ci_path);

        // Return the file data if requested
        if ($_ci_return === TRUE)
        {
            $buffer = ob_get_contents();
            @ob_end_clean();
            return $buffer;
        }

        /*
         * Flush the buffer... or buff the flusher?
         * 处理视图嵌套视图: header.php body.php footer.php
         *
         * In order to permit views to be nested within
         * other views, we need to flush the content back out(退回) whenever
         * we are beyond(超过) the first level of output buffering so that
         * it can be seen and included properly by the first included
         * template and any subsequent ones. Oy!
         */
        if (ob_get_level() > $this->_ci_ob_level + 1)
        {
            // 输出并关闭
            ob_end_flush();
        }
        else
        {
            $_ci_CI->output->append_output(ob_get_contents());
            @ob_end_clean();
        }

        return $this;
    }


文章作者: Fengit
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Fengit !
  目录