![]() |
Quick question on including data in view - 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: Quick question on including data in view (/showthread.php?tid=36613) |
Quick question on including data in view - El Forum - 12-08-2010 [eluser]Cameron Steele[/eluser] Let's say I'm setting a variable in my controller that I use in my view called "header" to set the title of the page. Assuming that (for some reason) I only wanted to include the header view once, but I wanted to set the page title variable in an <i>if</i> statement after I included the header view, would there be anything wrong with including the data in the content view during the <i>if</i> statement? In other words: Code: $this->load->view('header'); If "header" view uses $this->data['pagetitle'], is this still legit code? Could I even include the data in the "footer" load? I would assume that the PHP happens and the variables get tossed into the collection of views prior to getting to the user's browser, but I just want to be sure there's nothing wrong with this. Thanks! Quick question on including data in view - El Forum - 12-09-2010 [eluser]nuwanda[/eluser] You mean an actual page title in your html head? You pass the page title to your view and then use it thus: Code: <title> That code is in my header view. Quick question on including data in view - El Forum - 12-09-2010 [eluser]Cameron Steele[/eluser] Sorry, I was probably unclear. I have that aspect functioning in my header view. My question was that if I have, say, three different views (header, content, footer), can I include data that the header view uses in the content view? Quick question on including data in view - El Forum - 12-09-2010 [eluser]cahva[/eluser] Yes you can, but in your example $this->data['pagetitle'] is defined after $this->load->view('header') and is not available in header view. $pagetitle would be available in the footer view though because $this->data['pagetitle'] is defined before that. But remember to pass $this->data to other views also or use $this->load->vars($this->data) which will enable all the variables to all views without sending it separately. For example: Code: $data = array('foo' => 'Bar'); Variable $foo would be available in all the views and you would not need to send the array separately to all views that need it. Like this: Code: $data = array('foo' => 'Bar'); |