views:

366

answers:

5

Pretty often I need to access config variables in views. I know I can pass them from controller to load->view(). But it seems excessive to do it explicitly.

Is there some way or trick to access $config variable from CI views without disturbing controllers with spare code?

+1  A: 

You can do something like that:

$ci = get_instance(); // CI_Loader instance
$ci->load->config('email');
echo $ci->config->item('name');
Shein Alexey
Actualy within a view $this refers to CI_Loader and get_instance() refers to the CI_Base() as always.
Phil Sturgeon
+6  A: 

$this->config->item() works fine...

Phil Sturgeon
For example if there's `$config['foo'] = 'bar';` in the config using `$this->config->item('foo')` will be 'bar'.
Sasha
A: 

Whenever I need to access config variables I tend to use: $this->config->config['variable_name'];

The website-lab
A: 

Your controller should collect all the information from databases, configs, etc. There are many good reasons to stick to this. One good reason is that this will allow you to change the source of that information quite easily and not have to make any changes to your views.

Utah_Dave
+1  A: 

Also, the Common function config_item() works pretty much everywhere throughout the CodeIgniter instance. Controllers, models, views, libraries, helpers, hooks, whatever.

Phil Sturgeon