display common data accross all pages |
[eluser]InsiteFX[/eluser]
Code: $this->load->vars($this->data); Puts all the $data variables into CodeIgniters global cachced variables array. Which means they are avaiable anywhere in your application. Then you do not need to pass them to your views. See: the CodeIgniter Loader Class. InsiteFX
[eluser]John_Betong_002[/eluser]
@InsiteFX I have read the User Guide many times on the Loader Class and unfortunately do not appreciate the implications. // MY_Controller -> __construct -> $this->today I get the impression that setting the above variable defaults to a public class variable and therefore $this->today is available in all controllers and views (but not models). // $data['today'] = array('1','2','3'); // MY_Controller -> __construct -> $this->load->vars($data); I just tried the above and $today was not available in a sub-controllers but was available in my view? I think I will stick with setting a MY_Controller -> Public $variable.
[eluser]jmadsen[/eluser]
"this function produces the same result as using the second parameter of the $this->load->view() function...if you would like to set some global variables in the constructor of your controller and have them become available in any view file loaded from any function. " from the core Loader class comments itself, "Once variables are set they become available within the controller class and its "view" files." I'm also curious to dig deeper into the code behind it - I confirmed that only the loading controller seems to know about it, not any sub-controllers. that would really limit its usefulness, as far as I can tell.
[eluser]John_Betong_002[/eluser]
Further tests have revealed $this->load->vars(array()); if used in a controller's __construct() make all the array variables available in that particular controller methods and called views. Since my dynamic $today array is used in nearly every view and numerous controllers, it seems sensible to declare a unique public $today variable in the common MY_Controller class. The conclusion reached is that individual view variables can be set in a particular controller and these will be passed to the called view. I think that I will stick to using the $this->load->view('view', $data, TRUE); technique but later will no doubt think of a benefit when the load->var tool will be useful.
[eluser]DrDave[/eluser]
I too have been struggling with this for some time. I was finally able to get the variables available to all views using MY_Controller, but they are not available in any sub-controllers. There is one controller where I need to be able to make a modification to the categories. Is there a way to access this variable in that controller? MY_Controller Code: class MY_Controller extends CI_Controller {
[eluser]John_Betong_002[/eluser]
Read and digest my previous threads where I mentioned that vars loaded in MY_Controller->__construct() are visible in your views but not visible in your controllers. Try this: // one_controller.php Code: function whatever()
[eluser]InsiteFX[/eluser]
Here is something that I have been playing with, still working on it but it does work! You can also store array's if you serialize and unserialize them. Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); InsiteFX
[eluser]CodyPChristian[/eluser]
Not to revive a older thread but I dont see any of you trying the method that I use: http://learn-codeigniter.com/blog/passin...ded-views/ Hope that helps |
Welcome Guest, Not a member yet? Register Sign In |