EllisLab text mark
Advanced Search
     
Nested Views Rendering Out of Order
Posted: 06 April 2011 08:25 PM
Joined: 2011-04-06
3 posts

Hi all,
I’m very new to CI, but it’s been working great so far. My only problem seems to stem from nested views; some are rendering out of order. Currently, I have a login form that is supposed to display in my “header,” but is instead rendering before the entire page. I’ve read some older topics in the forums regarding this (http://ellislab.com/forums/viewthread/88335/ and http://ellislab.com/forums/viewthread/113358/#573470), as well as some guides online, but I’m still not sure I understand best practice.

What I’m trying to do is create a view that is essentially a template for 90% of the pages on my site, simply so that I don’t have to load multiple views in the controllers each time (i.e. header, content, footer). template.php looks like this:

<?php
$this
->load->view('includes/header');

$this->load->view($main_content);

$this->load->view('includes/footer');
?> 


All controller functions will set the necessary variables and load this file using:

$this->load->view('template'$data); 


At this point, everything is displaying properly.
header.php looks like this:

[all htmlheadbodyetc...]
<div id="utility">
<?php if(modules::run('membership/_isLoggedIn'FALSE)) : ?>
    
...
    
various data
    
...
<?php else : ?>
    <?php $this
->load->view('membership/login_form'); ?>
<?php 
endif; ?>
</div


Finally, login_form.php is something like this:

<div id="login_form">
echo 
form_open('blah');
echo 
form_input(...)
...
echo 
form_close();
</
div


This is obviously shortened, but essentially if the user is logged in, data is displayed. If not, a login form is displayed in the header.


The issue arises here. When the page is rendered, the login form is displayed at the very top, even above the <!DOCTYPE> and everything else in header.php. Why would this be rendering out of order? Should I not be using nested views like this? If not, what is the appropriate method?

If it changes anything, I’m running HMVC.

Thanks for your help!

edit:
Forgot to mention one other piece of information.  If the use is logged in, the data is displayed correctly in the header. It is only out of order when loading the login_form view.

 
Posted: 06 April 2011 08:37 PM   [ # 1 ]   [ Rating: 0 ]
Joined: 2011-04-06
3 posts

Forgot to mention one other piece of information.  If the use is logged in, the data is displayed correctly in the header. It is only out of order when loading the login_form view.

Thanks again.

 
Posted: 07 April 2011 12:40 AM   [ # 2 ]   [ Rating: 0 ]
Joined: 2008-04-03
124 posts

When output shows up earlier than expected, it usually points to something being echo’ed out rather than being put into CI’s output buffer.

Are you familiar with the optional third parameter when loading a view? It allows you to return teh view as a string to be output later.

From the docs:

There is a third optional parameter lets you change the behavior of the function so that it returns data as a string rather than sending it to your browser. This can be useful if you want to process the data in some way. If you set the parameter to true (boolean) it will return data. The default behavior is false, which sends it to your browser. Remember to assign it to a variable if you want the data returned:
$string = $this->load->view(‘myfile’, ‘’, true);

I don’t know if it’s related to HMVC shenanigans, or standard behavior for this situation, but when you are loading the login form view, the echos are being output directly to the browser.

You could try variations on returning the string and echoing that…

<?php $login_form $this->load->view('membership/login_form'''TRUE); ?> 
 
Posted: 07 April 2011 01:45 AM   [ # 3 ]   [ Rating: 0 ]
Joined: 2011-04-06
3 posts

Thank you, fesweb! Answered perfectly.

I didn’t realize there was an optional third parameter that would return the string, and that fixed everything. I definitely knew it had to do with the echo statements because of all the related threads on the matter, but I wasn’t able to find a fix until now.  Thanks again, if this was reddit or something, I’d give you an upvote.

For anyone else wondering, the fix in header.php was simply in the else statement:

<?php echo $this->load->view('membership/login_form'''TRUE); ?>