Welcome Guest, Not a member yet? Register   Sign In
Clearing $_ci_cached_vars in Loader.php
#1

[eluser]tmountain[/eluser]
Hi,

I have a use case where I'm sending multiple emails to different recipients in a single script execution. The email templates are rendered in this fashion:

Code:
foreach ($recipients as $recipient) {
    // set $data as needed for each recipient
    $message = $this->load->view("emailTemplates/$template", $data, true);
    // send the message
}

I'm running into an issue with templates populating incorrectly because CI_Loader keeps old variables cached inside of $_ci_cached_vars. In some cases, this is causing recipients to receive data that isn't related to their email.

What's the best strategy for completely clearing any cached data between calls to $this->load->view()? It'd be preferable not to modify the CI source code (for obvious reasons).
#2

[eluser]tmountain[/eluser]
I've stumbled onto this as a potential solution. Is dumping this into MY_Loader.php a good fix?

Code:
class MY_Loader extends CI_Loader
{
    public function clearCachedVars()
    {
        $this->_ci_cached_vars = array();
    }
}
#3

[eluser]Aken[/eluser]
That's one solution you can do if you want, yeah.

You should be able to do something like this, also:

Code:
$this->load->vars($data);
$this->load->view('view', null, true);

Just make sure your $data array overrides each value that you need reset. $this->load->vars() modifies the _ci_cached_vars array directly, rather than using array_merge().




Theme © iAndrew 2016 - Forum software by © MyBB