CodeIgniter Forums
Loading Views within Views - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Loading Views within Views (/thread-10331.html)



Loading Views within Views - El Forum - 07-27-2008

[eluser]hyeteck[/eluser]
I'm new to codeigniter and the whole MVC concept. I'm trying to test some stuff out and they aren't working out the way i thought they would.

This is the html i have

Code:
<div id="wrapper">
        <div id="container">
            <div id="header"><p>hello</p>&lt;?= $header ?&gt;</div>
            
            <div id="left_menu">&lt;?= $left_menu ?&gt;</div>
            
            <div id="content">&lt;?= $content ?&gt;</div>
            
            <div id="right_menu">&lt;?= $right_menu ?&gt;</div>
            
            <div id="footer">&lt;?= $footer ?&gt;</div>
        </div>
    </div>

and here is the controller class

Code:
&lt;?php
class Content extends Controller {
    
    function index()
    {
        $data['title'] = "This is the title";
        $data['header'] = $this->load->view('header');
        $data['left_menu'] = $this->load->view('left_menu');
        $data['content'] = $this->load->view('about_us');
        $data['right_menu'] = $this->load->view('right_menu');
        $data['footer'] = $this->load->view('footer');
        $this->load->view('template', $data);
    }    
}
?&gt;

i put in the "hello" paragraph in the html before the header is loaded but when the site is loaded, the "hello" text appears at the very end of the page after all the views are loaded. Why is this happening?

thanks


Loading Views within Views - El Forum - 07-27-2008

[eluser]freshface[/eluser]
Do it like this.

Code:
$this->load->view('footer',null,TRUE);



Loading Views within Views - El Forum - 07-27-2008

[eluser]hyeteck[/eluser]
thanks!

that worked but what are the additional fields for? What does the "null" and "TRUE" do?


Loading Views within Views - El Forum - 07-27-2008

[eluser]Nathan Moore[/eluser]
Hyteck -

By adding the TRUE value as the third argument, it returns the loaded view content into the variable instead of displaying it.


This will load the footer content into the variable $data['footer']:
Code:
$data['footer'] = $this->load->view('footer',null,TRUE);



Loading Views within Views - El Forum - 07-27-2008

[eluser]Nathan Moore[/eluser]
The first argument is the reference to the view you wish to load.

The second argument is if you need to pass any variables to the view (like your $data array)

The third argument (either TRUE or FALSE) tells CI to either return the content of the view (if TRUE) or echo it to the browser (if FALSE)


Loading Views within Views - El Forum - 07-27-2008

[eluser]hyeteck[/eluser]
Thanks alot, everything makes perfect sense now.


Loading Views within Views - El Forum - 07-27-2008

[eluser]Colin Williams[/eluser]
I've got a nifty little library that handles this in a uniformed fashion, among other things. PM me if you're interested