Jon*_*nas 5 variables codeigniter
我在控制器中有以下代码:
$data['what'] = 'test';
$this->load->view('test_view', $data);
$this->load->view('test_view');
Run Code Online (Sandbox Code Playgroud)
看法:
<?php
echo $what;
?>
Run Code Online (Sandbox Code Playgroud)
运行此代码时的结果是:
testtest
Run Code Online (Sandbox Code Playgroud)
难道不应该只是“测试”吗,因为第二次我没有传递变量 $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!
*
*/
Run Code Online (Sandbox Code Playgroud)
和:
/*
* 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]);
}
}
Run Code Online (Sandbox Code Playgroud)
这应该在变量使用完毕后从缓存中删除它们。
错误报告:http://bitbucket.org/ellislab/codeigniter/issue/189/code-igniter-views-remember-previous
这很有趣,我从来没有像这样使用它,但你是对的,它不应该这样做,也许这是一些缓存选项。在最坏的情况下,你必须这样称呼它:
$this->load->view('test_view', '');
Run Code Online (Sandbox Code Playgroud)
编辑:
我刚刚从他们的存储库中检查了 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)
Run Code Online (Sandbox Code Playgroud)
如果我理解正确的话,不幸的是你必须这样做:
$this->load->view('test_view', array('what' => ''));
Run Code Online (Sandbox Code Playgroud)