1

I've got a problem I have only recently noticed in my code ingiter app. I've stripped down the issue to try to locate the problem, but i'm still lost.

I have the following view (template/assets/box):

<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>

Then, for debugging purposes, i've adding the following into a controller:

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

however, when I load this controller, I get the following:

<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>

Basically, each call to the view seems to cascade the previous variables to the view unless overwritten. Is this a bug in codeigniter? Is it something that maybe PHP or CI config might be causing issues? I can't think of anything I've done which might have caused this problem.

1
  • if you are done with variable just unset() it Commented Sep 11, 2013 at 10:54

1 Answer 1

2

It's a feature. Sequential view loads have access to all the previous variables that you passed to the previous views.

Here's a comment from the CI source code that sorta demonstrates this fact:

/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.
*/
Sign up to request clarification or add additional context in comments.

4 Comments

Could you link to any reference of this in documentation, or code comment regarding this?
@RobinCastlin, no, sorry. Only based on past experience. It has always worked like this. You could always ask the codeigniter community itself for clarification, but that's the way it works.
If dug it out myself: /system/core/Loader.php line 800.
@RobinCastlin, Cheers, incorporated that actual quote into the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.