我在控制器中有以下代码:
$data['what'] = 'test';
$this->load->view('test_view', $data);
$this->load->view('test_view');
查看:
<?php
echo $what;
?>
运行此代码的结果是:
testtest
难道不应该简单地“测试”吗,因为第二次我没有传递变量 $data? 我怎样才能让 CodeIgniter 这样做?
编辑1:
我想出了一个临时解决方法来解决这个问题:
在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]);
}
}
这应该在变量使用完毕后从缓存中删除它们。
错误报告:http://bitbucket.org/ellislab/codeigniter/issue/189/code-igniter-views-remember-previous
这很有趣,我从来没有像这样使用它,但你是对的,它不应该这样做,也许这是一些缓存选项。在最坏的情况下,你必须这样称呼它:
$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' => ''));
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();
这将清除缓冲区并解决问题。