1

I want to use a custom load->view that once created will load a group of views for the page. It would have the same functionality of the $this->load->view() but have the ability to pre-load other views as well. How would I write this and have it work? thanks

<?php

class MY_Loader extends CI_Loader
{
    function __construct()
    {
        parent::__construct();
    }

    function admin_view($load_page, $vars=array())
    {
        $this->load->view('admin/header');
        $this->load->view($load_page);
        $this->load->view('admin/footer');
    }

    function members_view()
    {
        //same purpose as admin view
    }

    function public_view()
    {
        //same purpose as admin view
    }


}
Exploit
  • 6,278
  • 19
  • 70
  • 103
  • Please see a [previous answer](http://stackoverflow.com/questions/9540576/header-and-footer-in-codeigniter/9540985#9540985) of mine. – Jordan Arsenault Feb 11 '13 at 06:24

2 Answers2

0

This looks right, except that you don't need to call $this->load because you're already "in" the CI_Loader (through your extension), so you might just try:

function admin_view($load_page, $vars=array())
{
    $this->view('admin/header');
    $this->view($load_page);
    $this->view('admin/footer');
}

Meaning that you have access to the view method of the CI_Loader because you have extended that with your class.

swatkins
  • 13,530
  • 4
  • 46
  • 78
  • i want to know when i write $this->load->admin_view() in my controller how code igniter determines to call the correct method of respective class? @swatkins – ahmad05 Nov 22 '13 at 07:28
  • This is a mixture of how CI allows you to extend the CI core and basic class inheritance. By creating a `MY_Loader` class and putting it in the right place, CI will load that instead of its `CI_Loader` class - and because `MY_Loader` extends `CI_Loader` - all of its methods are available. So `$this->load` is an instantiation of the `MY_Loader` class. Therefore, calling `$this->load->admin_view();` calls the `admin_view` method of the `MY_Loader` class. – swatkins Nov 22 '13 at 16:28
  • Thank you so much i had been wasting two or three days to figure it out how it works...you helped my cause and the extended loader class to be of exact name "MY_loader" right? – ahmad05 Nov 25 '13 at 05:56
  • 1
    Yes, you would name your file `MY_Loader.php`. If you extend any of the core files, then you rename your file based on the prefix set in the config file and the name of the core class (minus the "CI_" part). By default, the prefix is "MY_" (this is set on line 136 of the application/config/config.php file: '$config['subclass_prefix'] = 'MY_';') – swatkins Nov 25 '13 at 16:30
0

I used to have a template.php in views/_includes like this:

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

So the $view I want to load is passed throught the Controller. For example:

class News extends MY_Controller {
    public function index()
    {
        $data['view'] = 'admin/news/index';
        $data['results'] = $this->news_model->get_all);
        $this->load->view('admin/_includes/template', $data);
    }