Cii Learner
Cii Learner

Reputation: 161

How to load view commonly in code igniter

I am developing an application in code igniter.

How can i load a view commonly so that i don't need to load it individually for all the functions ?

Here is the code :

controller.php

public function index()
    {
        $this->load->view('index');
        $this->load->view('css');   
        $this->load->view('js');
    }
public function about()
        {
            $this->load->view('about');
            $this->load->view('css');   
            $this->load->view('js');
        }

Here i can load the $this->load->view('css'); and $this->load->view('js'); into common file called $this->load->view('comp'); and load it.

But what i want to know is i don't need to do it for each functions. How can i do it commonly ?

something like

class site extends CI_Controller 
{

  **  Here the view should be loaded  $this->load->view('comp');
public function index()
{
$this->load->view('about');.
}

How can i do this ?

Upvotes: 1

Views: 63

Answers (2)

Rakesh Sharma
Rakesh Sharma

Reputation: 13728

create a function:- you can use it commonly write function in that file which included commonly

public function loading_views($view) {
  $this->load->view($view);
  $this->load->view('css');   
  $this->load->view('js');
}

and then your functions will be

public function index()
    {
        $this->loading_views('index');
    }
public function about()
    {
        $this->loading_views('about');
    }

Upvotes: 0

Raggamuffin
Raggamuffin

Reputation: 1760

I use a method on a base controller called showPage:

MY_Controller extends CI_Controller {

  protected function showPage($template,$data) {
     $this->load->view('css');   
     $this->load->view('js');
     $this->load->view($template,$data);
  }
}

Then just in any controller call: $this->showPage('about',$data);

Upvotes: 1

Related Questions