代码点火器视图记住以前的变量!

时间:2010-10-12 21:40:43

标签: variables codeigniter

我在Controller中有以下代码:

$data['what'] = 'test';
$this->load->view('test_view', $data);
$this->load->view('test_view');

查看:

<?php
    echo $what;
?>

运行此代码时的结果是:

testtest

不应该只是'测试',因为第二次我没有传递变量$ data? 如何使CodeIgniter以这种方式运行?

EDIT1:

我已经提出了解决此问题的临时解决方法:

在Loader.php中替换:

/*
* Flush the buffer... or buff the flusher?
*
* 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!
*
*/ 

使用:

 /*
 * Flush the buffer... or buff the flusher?
 *
 * 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 (is_array($_ci_vars)){
   foreach ($_ci_vars as $key12 => $value12) {
      unset($this->_ci_cached_vars[$key12]);
   }
 }

这应该在使用完毕后从缓存中删除它们。

BUG REPORT:http://bitbucket.org/ellislab/codeigniter/issue/189/code-igniter-views-remember-previous

2 个答案:

答案 0 :(得分:1)

这是非常有趣的,我从来没有像这样使用它,但你是对的,不应该这样做,也许这是一些缓存选项。在最坏的情况下,你必须这样称呼它:

$this->load->view('test_view', '');

编辑:

我刚刚从他们的存储库中检查了Code Igniter代码。原因是他们真的在缓存变量:

    /*
     * 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)

如果我理解正确,你必须这样做:

$this->load->view('test_view', array('what' => ''));

答案 1 :(得分:0)

codeigniter默认情况下会这样做。我正在搜索原因,然后找到了

empty($_ci_vars) OR $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
extract($this->_ci_cached_vars);

关于加载程序类,但是您可以使用

$this->load->clear_vars();

这将清除缓冲区并解决问题。