Welcome Guest, Not a member yet? Register   Sign In
Nested Views Rendering Out of Order
#1

[eluser]nickp[/eluser]
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:
Code:
<?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:
Code:
$this->load->view('template', $data);


At this point, everything is displaying properly.
header.php looks like this:
Code:
[all html, head, body, etc...]
<div id="utility">
&lt;?php if(modules::run('membership/_isLoggedIn', FALSE)) : ?&gt;
    ...
    various data
    ...
&lt;?php else : ?&gt;
    &lt;?php $this->load->view('membership/login_form'); ?&gt;
&lt;?php endif; ?&gt;
</div>


Finally, login_form.php is something like this:
Code:
<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.
#2

[eluser]nickp[/eluser]
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.
#3

[eluser]fesweb[/eluser]
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:
Quote: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...
Code:
&lt;?php $login_form = $this->load->view('membership/login_form', '', TRUE); ?&gt;
#4

[eluser]nickp[/eluser]
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:
Code:
&lt;?php echo $this->load->view('membership/login_form', '', TRUE); ?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB