Welcome Guest, Not a member yet? Register   Sign In
Loading Views within Views

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

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

and here is the controller class

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);

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?


Do it like this.



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

[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']:
$data['footer'] = $this->load->view('footer',null,TRUE);

[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)

Thanks alot, everything makes perfect sense now.

[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

Theme © iAndrew 2016 - Forum software by © MyBB