Questions about the fragment source code in Codeigniter 2.2 |
I am struggling with Loader class, I asked a question on Stackoverflow, I hope someone can take a look.
Thanks The link: http://stackoverflow.com/questions/36845...gniter-2-2 Quote:I am trying to read the source code of CI 2, I am confused about this In view method of core/Loader class: If you load a view which does not contain a nested view, it is loaded into the buffer, the buffer's contents are appended to the output, the buffer is cleaned, and output buffering is turned off. This whole process is the same every time you call $this->load->view(), unless the loaded view contains another call to $this->load->view(). If you call $this->load->view() from within a view (which was itself loaded by a call to $this->load->view()), you end up with a stack of buffers (depending on the nesting of the views) which has to be flushed to be processed properly by the output handler. No matter how the views are nested, the code you are looking at is always dealing with the buffer of the current view. If it's a nested view, it's flushed with ob_end_flush(), if it's a top-level view, it is passed to the output handler via ob_get_contents() and the output buffer is closed with ob_end_clean(). The reason this works is that nested views will cause the Loader's method to be called when the view is placed in the buffer, so the Loader waits for the nested views to be processed before it finishes processing the original view. Quote:2nd: why we need to flush the earliest buffer instantly? Each buffer is flushed when the loader is done with it, there's no auto-flushing going on. The loader uses output buffering to ensure the views are loaded and processed in the proper order and suppresses the output by flushing the buffer to a string stored in the Output class until output is started by the framework. Quote:3rd: Why the condition is the current ob level greater than the default level +1? The loader always closes the buffer after a view has been completely processed. ob_end_flush() is only called for nested views, because the buffer has to remain open until it gets back to processing the original view. Quote:4th: If a view is flushed manually here, would it still be adjusted by Output->_display()?More than likely, if you start manually flushing the buffer, you'll create an error somewhere. It is possible to use output buffering in a CI application without causing problems, but you have to be extremely careful, and, in most cases, avoid using $this->load->view() until you've finished dealing with the buffer. If you want Output->_display() to handle the output, you have to use Output->append_output() or Output->set_output() for anything which isn't processed by $this->load->view(). It's usually best to let CI manage the output buffer by only performing output in your views. |
Welcome Guest, Not a member yet? Register Sign In |