Part of the EllisLab Network
   
 
variables persisting across multiple view loads
Posted: 05 June 2008 03:39 AM   [ Ignore ]  
Summer Student
Total Posts:  19
Joined  12-11-2007

I’m not really sure whether this is a ‘bug’, but from a usability standpoint I think it would make sense for things to function as I’m about to describe.

Currently the variables that are extracted for use within views will persist across multiple calls to the load->view() function, if you use many views, and each view has similar page elements on it, it’s difficult to keep all of the variable names straight and make sure you don’t reuse them. For example:

Index function within Controller:

function index()
{
    $data[
'print'] = true;
    
$data['content'] = "content";
    
$this->load->view('debug',$data);
    
$other_data['content'] = "other content";
    
$this->load->view('debug',$other_data);
}

Debug View file:

<p><?php
    
if(isset($print))
        
print_r($content);
?></p>

Browser output:

content

other content

Expected output:

content

I know this seems trivial from the example, but in my case I have a ‘Pagebuilder’ library which loads in many views (header, navigation, content, footer) however one of the views and its associated data is specified outside of the library, as seen in the following example:

function index()
{
    $this
->load->model('blog');
    
$entries = $this->blog->get_entry_items(10);
    
$data['content'] = $entries;
    
//this function loads many other views besides the 'blog' view specified
    
$this->pagebuilder->build('blog',$data);
}

I ran into a problem earlier where I had collisions with the names of the variables passed to the views.  It’s a problem that’s easily solved by keeping track of all the variable names used within the views, but things working like this breaks the encapsulation of my Pagebuilder class.  Ideally each of the variables passed to a view would be unset (or through some other method made inaccessible to other views) after that view is finished loading.

Anyone think this qualifies as a bug?

 Signature 

blog | twitter

Profile
 
 
Posted: 05 June 2008 08:56 AM   [ Ignore ]   [ # 1 ]  
Administrator
Avatar
RankRankRankRankRankRank
Total Posts:  6593
Joined  03-23-2006

You’ve got a really long post here, and I confess I haven’t read the whole thing… but Flashdata?

 Signature 

DerekAllard.com - CodeIgniter, ExpressionEngine, and the World of Web Design
BambooInvoice - Open Source, CodeIgniter powered invoicing.

Profile
MSG
 
 
Posted: 05 June 2008 09:04 AM   [ Ignore ]   [ # 2 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  3169
Joined  07-14-2006

In short the problem is that the $content variable displays the value from both arrays when the view is called for the second time. I had to read it more than once to understand the problem yelirekim was explaining smile

Profile
 
 
Posted: 05 June 2008 10:01 AM   [ Ignore ]   [ # 3 ]  
Administrator
Avatar
RankRankRankRankRankRankRank
Total Posts:  15291
Joined  06-03-2002

This is expected behavior.  Once variables are set they become available within the controller class and its view files.  Sending an array in $this->load->view() is the same as sending an array directly to $this->load->vars() before calling the view file.  This simplifies things for most people using multiple views in a controller.  If you are using multiple view files in a single controller and want them to each have their own set of variables exclusively, you’ll need to manually clear out the $this->load->_ci_cached_vars array between view calls.

A code comment in the Loader class describes another situation showing why this is the desired default behavior:

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

 Signature 
Profile
MSG
 
 
Posted: 05 June 2008 10:11 AM   [ Ignore ]   [ # 4 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  3169
Joined  07-14-2006

Good to know but maybe it should be mentioned in the user guide too?

Profile
 
 
Posted: 05 June 2008 11:32 AM   [ Ignore ]   [ # 5 ]  
Summer Student
Total Posts:  19
Joined  12-11-2007

Just by standard conventions, $this->load->view(‘view_name’,$data) would indicate that $data persists within the scope of the view function, so it’s a little counter-intuitive in that sense.  I kind of agree with xwero that it should be noted in the user guide.  I can also see how this situation isn’t going to apply to most people.

Thanks for the replies.

 Signature 

blog | twitter

Profile
 
 
Posted: 05 June 2008 11:37 AM   [ Ignore ]   [ # 6 ]  
Administrator
Avatar
RankRankRankRankRankRankRank
Total Posts:  15291
Joined  06-03-2002

An addition to the user guide is a fine suggestion, but I disagree with you about which case is more common, based both on personal experience with building sites with CI as well as from others.  The architecture you are proposing is a rather unique approach to using multiple view files, both in it being undesirable for each to have access to the others’ variables, as well as using non-unique variable names for different contexts.

 Signature 
Profile
MSG
 
 
Posted: 05 June 2008 02:05 PM   [ Ignore ]   [ # 7 ]  
Summer Student
Total Posts:  19
Joined  12-11-2007

I was agreeing with you about the more common scenario, probably just phrased it wrong wink

 Signature 

blog | twitter

Profile
 
 
Posted: 05 June 2008 02:14 PM   [ Ignore ]   [ # 8 ]  
Administrator
Avatar
RankRankRankRankRankRankRank
Total Posts:  15291
Joined  06-03-2002

My misunderstanding!

 Signature 
Profile
MSG
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 62653 Total Logged-in Users: 34
Total Topics: 77188 Total Anonymous Users: 0
Total Replies: 416682 Total Guests: 230
Total Posts: 493870    
Members ( View Memberlist )