How to have some data always available at views - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: How to have some data always available at views (/showthread.php?tid=27464) |
How to have some data always available at views - El Forum - 02-11-2010 [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 How to have some data always available at views - El Forum - 02-11-2010 [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 How to have some data always available at views - El Forum - 02-11-2010 [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? How to have some data always available at views - El Forum - 02-11-2010 [eluser]cahva[/eluser] Better to use $this->load->vars() Code: $array = array( How to have some data always available at views - El Forum - 02-11-2010 [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. How to have some data always available at views - El Forum - 02-11-2010 [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. How to have some data always available at views - El Forum - 02-11-2010 [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-MY_Controller/blob/master/MY_Controller.php How to have some data always available at views - El Forum - 02-11-2010 [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. How to have some data always available at views - El Forum - 02-11-2010 [eluser]Jarno[/eluser] Can someone explain this MY_Controller please? It seems some kind of "core" which is available through the whole project. How to have some data always available at views - El Forum - 02-11-2010 [eluser]jbreitweiser[/eluser] Its in the manual. Give it a read. http://ellislab.com/codeigniter/user-guide/general/core_classes.html |