views:

1480

answers:

3

Hello,

I am using $data in all my views $this->load->view('my_view', $data);

I have also autoload a Controller following this guide Extending Core Controller

But I want to make $data global because in views there is a sidebar which is constant for whole project and displays info fetched through db in autoloaded controller

Currently I have to manually write $data['todo'] for each and fetch info from autoloaded model.

Thank You.

+1  A: 

added

var $data to my autoloaded controller

and replaced $data to $this->data

Shishant
+1  A: 

I ran into a similar problem earlier today. I found that an easier way, rather than globals, was to use constants. You can define a constants file that will load from your index.php file:

// Include additional constants
$defines_file = 'includes/defines.php';
if (file_exists($defines_file))
{
    require_once($defines_file);
} 

Then you can add your constants to the defines.php file:

define(MY_CONSTANT,'my constant info');

This way they will be available in any file throughout the system either directly: echo MY_CONSTANT; or you can assign them to variables.

I decided this way would be easier for me as I would only have 1 location to go to when/if I needed to change the constants.

More: http://codeigniter.com/forums/viewthread/56981/#280205

stormdrain
Its good idea and I am already using for all constant values but this is not suitable for me because I am getting the data from db which is different everytime. Using another include script wont let me use inbuilt CI database lib so I had to go this way
Shishant
A: 

Rather than making the view data global, I'd recommend using HMVC to build a module to produce this sidebar view. HMVC is a nice clean way of coding partial views.

Stephen Curran
HVMC is overkill when you're talking about something as simple as partial views. If you have a partial view that doesn't change much, just load the view into the constant itself: `define('MY_VIEW', $this->load->view('my_view', '', TRUE));` - then simply echo the MY_VIEW constant in your larger view. HVMC adds a lot of baggage for something as simple as this.
b. e. hollenbeck
Where in the code do you load the data needed to render the partial view?
Stephen Curran