Search code examples
codeignitercodeigniter-2

Load a view inside another view


I've been using django for some time and I decided to start a new project but this time in Codeigniter, I used to extend the template file in my views and put content inside the {% block content %} block but it seens to be different in CodeIgniter.

In CodeIgniter I have something like:

<?php
     $this->load->view('header');
     $this->load->view('form_add_customer');
     $this->load->view('footer');
?>

But is there a way to have an unique file with header, content and footer like this?

<html>
    <head><title>Test</title></head>
    <body>
        <div id="header">Welcome</div>
        <div id="content">
        </div>
        <div id="footer"> 2013 - Tectcom Telecom</div>
    </body>
</html>

And put a view file with a form inside the content div?


Solution

  • Update your html (layout) file like this:

    <div id="content"><?php $this->load->view($content) ?></div>
    

    In your controller, call the view like this:

    $view_data = array();
    $view_data['content'] = 'form_add_customer';
    $this->load->view('path/to/layout', $view_data);