Welcome Guest, Not a member yet? Register   Sign In
Quick question on including data in view
#1

[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 ($this->form_validation->run() == FALSE)
{
    $this->data['pagetitle'] = "Fail";
    $this->load->view('failure', $this->data);
}

else
{
    $this->data['pagetitle'] = "Not fail";
    $this->load->view('success', $this->data);
}

$this->load->view('footer');

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!
#2

[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:
&lt;title&gt;
  &lt;?php if(isset($page_title)) echo $page_title ?&gt;
&lt;/title&gt;

That code is in my header view.
#3

[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?
#4

[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');

$this->load->vars($data);

$this->load->view('header');
$this->load->view('content');
$this->load->view('footer');

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');

$this->load->view('header',$data);
$this->load->view('content',$data);
$this->load->view('footer',$data);




Theme © iAndrew 2016 - Forum software by © MyBB