Search code examples
phpcodeignitercodeigniter-2templating

Call 1 view in Codeigniter but share header/footer among all views?


Loving Codeigniter, but I'm finding it annoying that I have to call a header, footer and the main view for each controller.

What is the best way to share a header/footer among all views and minimize replication of code. I don't want to use a template engine.


Solution

  • create a view, let's call it template.php with this code

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

    And on your controller do this

    //the name of the view file you want to show, in my case I want to open the login_page.php view that's inside the folder user
    $data['view'] = 'user/login_page';
    //load the data variable so I can use it across the views
    $this->load->vars($data);
    //load the template.php view that will load header, user/login_page and footer
    $this->load->view('template');