CodeIgniter Forums

Full Version: Passing Data to View
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
I have a controller that sends an email, then loads a view:

$data['title'] = 'Contact Us';

Code:
$email_data = array('message' => 'This is the message';
$email_message = $this->load->view('email_template', $email_data, TRUE);

// send email...
// if there was a problem store error message in $data['message']

$this->load->view('contact', $data);

And in my view (contact.php):

Code:
<?php if(!empty($message)) : ?>
<?= $message ?>
<?php endif; ?>

Even if there's no problem sending the email, the
Code:
$message
variable is not empty -- it contains the email message that's passed to the email template.

So, even though the
Code:
$data
array that we're explicitly passing to the view we want to load doesn't contain an index/value for
Code:
'message'
, since there's a different array that does, and is passed to a different view, that variable is then available in a view to which we're not explicitly passing that array.

I realize that a really simple work around is to initialize any indexes of our "final" $data array that may possibly exist in any other $not_data arrays passed to views that may be loaded before loading our "final" view. But should I have to do this? Is this the intended behavior?

Thanks!

El Forum

[eluser]Aken[/eluser]
CI "caches" view variables in order to make variables available to views embedded inside views. So it's normal for that to happen, but I can see how it may not be what you expect.

You should create an issue, or even a pull request if you have a suggested solution, on Github. See if others have this problem or empathize enough to create a solution if you haven't suggested one.

El Forum

[eluser]Unknown[/eluser]
Sounds good. Thanks for the reply!