【问题标题】:Code Igniter Views Remember Previous Variables!Codeigniter 视图记住以前的变量!
【发布时间】:2010-10-12 21:40:42
【问题描述】:

我在控制器中有以下代码:

$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

【问题讨论】:

    标签: variables codeigniter


    【解决方案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' => ''));
    

    【讨论】:

    • 如果使用NULL作为参数会怎样?
    • NULL 作为参数也不起作用。还是一样的输出。
    • 我在 1.7.2 上,没有任何黑客或任何东西。全新安装。
    • 用我写的补丁更新了我的帖子。
    • @Jonas - 我来这里是为了和 Vinzenz 说同样的话。这是一个缓存问题。就在视图加载器启动输出缓冲区之前,它extracts$this-&gt;_ci_cached_vars。这意味着其中的所有变量都被放入本地范围,这允许您引用 $test。我不会真正称其为错误,因为 CI 确实通过将值存储在缓存中来节省大量内存。解决这个问题的方法是确保每次都将正确的值传递给您的视图,正如 Vinzenz 所建议的那样。
    【解决方案2】:

    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();
    

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

    【讨论】:

      猜你喜欢
      • 2013-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-10
      • 2023-03-09
      • 1970-01-01
      • 2011-09-17
      相关资源
      最近更新 更多