Why does CodeIgniter store output in a variable?

I recently looked at CodeIgniter code to see how it works.

One thing I don't understand is why does CodeIgniter store all the output generated by the views in a single variable and output it at the end of the script?

Here's a snippet of code. /system/core/Loader.php on line 870
CI source code @GitHub

/*
* Flush the buffer... or buff the flusher?
*
* 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!
*/
if (ob_get_level() > $this->_ci_ob_level + 1)
{
    ob_end_flush();
}
else
{
    $_ci_CI->output->append_output(ob_get_contents());
    @ob_end_clean();
}

The append_output function adds this string to a variable in the CI_Output class.
Is there a specific reason for this rather than using echo statements or is it just a personal preference?

+3
source share
2 answers

There are several reasons. For a reason, you can download the view and return it, and not 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

TRUE , . , :

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

, , $this->output->set_content($content), - ( , , , ), ( ) .

, echo print ( Wordpress ). echo $class->method();, , , - .

+6

.

/**
* 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!
*/

:

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

All Articles