How to have some data always available at views |
[eluser]Jarno[/eluser]
Hello, I'm new to CodeIgniter and I'm wondering how it's possible to always have some variables available at every view. For example I set the site title in the database and want to display it in every view. Yours, Jarno
[eluser]Sarfaraz Momin[/eluser]
use a config item add the title to your config file .. something like below... Code: $config['site_title'] = "This is my site title"; use it in your views like this... Code: $this->config->item['site_title']; -Sarfaraz
[eluser]Jarno[/eluser]
[quote author="Sarfaraz Momin" date="1265935870"]use a config item add the title to your config file .. something like below... Code: $config['site_title'] = "This is my site title"; use it in your views like this... Code: $this->config->item['site_title']; -Sarfaraz[/quote] What if the title is saved in a database?
[eluser]cahva[/eluser]
Better to use $this->load->vars() Code: $array = array(
[eluser]Jarno[/eluser]
Where to define that piece of code? I'm new to CI, I am used to my own little framework I created overtime, with Smarty. I read a lot in the user guide but I'm still not familiar with the files etc. so hopefully you can be very clear.
[eluser]cahva[/eluser]
$this->load belongs to loader class where you can find it in the user guide. You can use this in the constructor of your controller which would make those variables global in every function of that controller. Or define it in controller's function and it would be available only in that function.
[eluser]SpooF[/eluser]
I would suggest creating a MY_Controller and then extend all your controls from it. Here's one made by Ben Edmunds http://github.com/benedmunds/CodeIgniter...roller.php
[eluser]jbreitweiser[/eluser]
You could also make a model that reads the database and initializes itself with the data. Then just use it to get the different values. Make sure to load the model on every page or include it via MY_Controller.
[eluser]Jarno[/eluser]
Can someone explain this MY_Controller please? It seems some kind of "core" which is available through the whole project.
[eluser]jbreitweiser[/eluser]
Its in the manual. Give it a read. http://ellislab.com/codeigniter/user-gui...asses.html |
Welcome Guest, Not a member yet? Register Sign In |