【问题标题】:CodeIgniter view referencing previous view's call variablesCodeIgniter 视图引用前一个视图的调用变量
【发布时间】:2013-09-15 08:35:54
【问题描述】:

我最近在我的代码 ingiter 应用程序中发现了一个问题。我已经剥离了问题以尝试定位问题,但我仍然迷路了。

我有以下视图(模板/资产/框):

<div class="box <? if(isset($class)){echo $class;} ?>">

    <?
    if(isset($boxtitle) || isset($titleimg)){
        ?>
        <div class="head">
            <? if(isset($boxtitle)){ echo '<h3>'.$boxtitle.'</h3>';} ?>
            <? if(isset($titleimg)){ echo '<img src="'.$titleimg.'" />';} ?>
        </div>
        <?
        $has_head = 1;
    }
    ?>

    <div class="content <? if(!isset($has_head)){ echo 'border-top';}?> <? if(!isset($footer)){ echo 'border-bottom';}?>">
        <?
        if(isset($type)){
            switch($type){
                case 'list':
                    echo '<ul class="list" >';
                    foreach($items as $item){
                        ?>
                        <li><?=$item; ?></li>
                        <?
                    }
                    echo '</ul>';
                    break;
                case 'grid':
                    break;
                default:
                    echo '<div class="item">'.$content.'</div>';
                    break;
            }
        }
        else{
            echo '<div class="item">'.$content.'</div>';
        }

        ?>

    </div>


        <? 
        if(isset($footer)){
            ?><div class="footer <? if(isset($items)){echo 'border-top';} ?> "><?
            echo $footer; 
            ?></div><?
        } 
        ?>


</div>

然后,出于调试目的,我将以下内容添加到控制器中:

public function index(){

        // page data
        $data['page_title'] = 'Directories';

        // load template and output
        $this->load->view('template/assets/box',array('boxtitle'=>'foo','content'=>'bar')); 
        $this->load->view('template/assets/box',array('content'=>'blah'));
        $this->load->view('template/assets/box',array());
        #$this->Template_model->view('directories/index',$data);
    }

然而,当我加载这个控制器时,我得到以下信息:

<div class="box ">

            <div class="head">
            <h3>foo</h3>                    </div>

    <div class="content  border-bottom">
        <div class="item">bar</div>
    </div>




</div>


<div class="box ">

            <div class="head">
            <h3>foo</h3>                    </div>

    <div class="content  border-bottom">
        <div class="item">blah</div>
    </div>




</div>


<div class="box ">

            <div class="head">
            <h3>foo</h3>                    </div>

    <div class="content  border-bottom">
        <div class="item">blah</div>
    </div>




</div>

基本上,对视图的每次调用似乎都会将先前的变量级联到视图,除非被覆盖。这是codeigniter中的错误吗?是不是 PHP 或 CI 配置可能会导致问题?我想不出我做过的任何事情都可能导致这个问题。

【问题讨论】:

  • 如果你完成了变量 unset() it

标签: php codeigniter templates codeigniter-2


【解决方案1】:

这是一个功能。顺序视图加载可以访问您传递给先前视图的所有先前变量。

下面是来自 CI 源代码的注释,可以证明这一事实:

/system/core/Loader.php:840

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

【讨论】:

  • 您能否链接到文档中对此的任何参考,或有关此的代码注释?
  • @RobinCastlin,不,对不起。仅基于过去的经验。它一直是这样工作的。您可以随时要求 codeigniter 社区本身进行澄清,但这就是它的工作方式。
  • 如果我自己挖出来:/system/core/Loader.php line 800。
  • @RobinCastlin,干杯,将实际引用纳入答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 2011-02-06
  • 2012-09-08
  • 2018-02-21
  • 1970-01-01
  • 1970-01-01
  • 2017-10-07
相关资源
最近更新 更多