CodeIgniter Forums
Setting Global Template Variables - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Setting Global Template Variables (/showthread.php?tid=7516)



Setting Global Template Variables - El Forum - 04-12-2008

[eluser]Philip Pryce[/eluser]
Right well, i'm kinda new to coding in Code Ignitor, i've know most of the basics and such. There is still one thing that alludes me. How can i set some application wide variables?
Say i wanted to be able to give the site a dynamic title, so i can reference it in all the templates without passing it view the $this->load->view function.
I feel this should be easy, but i cant seem to find a way.


Setting Global Template Variables - El Forum - 04-12-2008

[eluser]m4rw3r[/eluser]
You could use a constant, see http://www.php.net/manual/en/function.define.php.
Or you can define the name as a property of the controller and then fetch it with get_instance().


Setting Global Template Variables - El Forum - 04-12-2008

[eluser]Developer13[/eluser]
From the controller:

$this->var = "foo";

From the view:

<?=$this->var;?>


Setting Global Template Variables - El Forum - 04-12-2008

[eluser]Philip Pryce[/eluser]
really i could do with it being site wide, so i didnt have to change stuff from all the controllers.


Setting Global Template Variables - El Forum - 04-12-2008

[eluser]m4rw3r[/eluser]
@developer13: Oo, never thought about that $this was the CI object (or contains references to CI object properties) in the view.


Setting Global Template Variables - El Forum - 04-12-2008

[eluser]m4rw3r[/eluser]
If you want to have it site-wide, use a constant in the config.php or something that always gets loaded, but you can't change the value once it's defined, so a property (or one defined in a base_controller) can be better.


Setting Global Template Variables - El Forum - 04-15-2008

[eluser]paradoxic[/eluser]
I am trying to do this as well. I found I can set a variable in config.php like so:
Code:
$config['image_url'] = "http://www.salviasource.org/guamap1/images/";

Then in my view page I can call the variable like so:
Code:
<?= $this->config->item('image_url'); ?>

This works however I am wondering if this is in any way inefficient or if there is a more effective way in doing this. It seems to me that it might be unnecessary to call the config file when there is another place to set global variables that automatically loads with every page.


Setting Global Template Variables - El Forum - 04-26-2008

[eluser]paradoxic[/eluser]
Can anyone give me any more advice?

Thanks.