Reputation: 16726
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.
Upvotes: 1
Views: 81
Reputation: 2625
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:
/*
* 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.
*/
Upvotes: 2