0

I do not know if the following is possible. If not other suggestions are appreciated.

In nearly all my controllers I will load some default views. For example header, footer and menu.

I would like to have certain variables auto load for each view.

If I take the header as an example. Into my header I will always load an array of $css scripts and an array of $javascript files.

$javascript[] = 'js/jquery.js'; $javascript[] = 'js/jqueryui.js';

But additionally, depending on the current page logic, I might want to add another javascript file to my $javascript variable.

$javascript[] = 'js/custom.js';

Then ideally, I would like these variables to be automatically inserted as data into the load of the view.

In other words, I just want to call:

$this->load->view('header');

How could this be achieved?

3 Answers 3

1

Create MY_Controller add a public array there then extend from MY_Controller

class MY_Controller extends CI_Controller  {
     public $data;

     function __construct() {
         parent::__construct();
         $this->data['MYVAR'] = 'Something';
     }
}

in your other controllers you just do it like this

class SomeClass extends MY_Controller {
    function __construct () {
        parent::__construct();
    }

     function index () {
         $this->data['SomeOtherVar'] = 'xxx';
         $this->load->view('viewname', $this->data);
     }
  }
Sign up to request clarification or add additional context in comments.

Comments

0

You can use $this->load->vars in your Controller. I use this in my_controller and all controllers are extend from MY_Controller For example

<?php
    class MY_Controller extends Controller{
        public function __construct(){
            parent::__construct();
            $this->setGlobalViewVariables();
        }

        public function setGlobalViewVariables(){
            $result = array();
            $result['value1'] = 'value1';
            $result['value2'] = 'value1'; 
            $this->load->vars($result);
        }
    }
?>

Comments

0

you should create an hook for this, it is very simple

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.