Welcome Guest, Not a member yet? Register   Sign In
Setting Global Template Variables
#1

[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.
#2

[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().
#3

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

$this->var = "foo";

From the view:

<?=$this->var;?>
#4

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

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

[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.
#7

[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.
#8

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

Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB