Why does CodeIgniter store output in a variable? Why does CodeIgniter store output in a variable? codeigniter codeigniter

Why does CodeIgniter store output in a variable?


There are several reason. On reason is that you can load a view and have it returned rather than output directly:

// Don't print the output, store it in $content$content = $this->load->view('email-message', array('name' => 'Pockata'), TRUE);// Email the $content, parse it again, whatever

The third parameter TRUE buffers the output so the result is not printed to screen. WIthout that you'd have to buffer it yourself:

ob_start();$this->load->view('email-message', array('name' => 'Pockata'));$content = ob_get_clean();

Another reason is that you cannot set headers after you have sent output, so for instance you can user $this->output->set_content($content), then afterwards at some point set the headers (set content type headers, start a session, redirect the page, whatever) then actually display (or don't display) the content.

In general, I find it very bad form to have any class or function use echo or print (common in Wordpress for one example). I'd almost always rather use echo $class->method(); than have it echo for me, for the same reasons outlined above - like being able to assign the content to a variable without it spilling into the output directly or creating my own output buffer.


the answer is in the commenting in your post.

/*** In order to permit views to be nested within* other views, we need to flush the content back out whenever* we are beyond the first level of output buffering so that* it can be seen and included properly by the first included* template and any subsequent ones. Oy!*/

it's so you can go:

$view = $this->load->view('myview', array('keys' => 'value'), true);$this->load->view('myotherview', array('data' => $view));