sfy
sfy

Reputation: 3228

Questions about the fragment source code in Codeigniter 2.2

I am trying to read the source code of CI 2, I am confused about this In view method of core/Loader class:

        /*
     * 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();
    }

I can't get the point of this fragment,

1st: The code comment says it is for nested views, but I think it will be flushed in the following case:

...
$this->load->view('section_a');
$this->load->view('section_b');
...

Every time we run the view method, a buffer will be opened, so if we load two views, even if they are not nested, the first one will be flushed, is that right?

2nd: why we need to flush the earliest buffer instantly?

Since they will be flushed automatically in the end, in fact I found there are no 'ob_end_flush' in the final render method,Output->_display(), that means CI still rely on the auto flushing function , right?

3rd: Why the condition is the current ob level greater than the default level +1?

If I load two views, the second view will trigger the flushing, right?

4th: If a view is flushed manually here, would it still be adjusted by Output->_display()?

I was trying my best to speak it clearly, I hope you can help me. Thank you.

Upvotes: 0

Views: 90

Answers (1)

DFriend
DFriend

Reputation: 8964

Every time we run the view method, a buffer will be opened, so if we load two views, even if they are not nested, the first one will be flushed, is that right?

No. It only would happen if while one view file is being loaded another call to $this->load->view() is called.

When called consecutively, as in your example, each will be appended to output class as they are loaded.

2nd: why we need to flush the earliest buffer instantly?

It isn't the earliest buffer that flushes it is the current buffer and it gets flushed into the earlier buffer. Output buffers are stacked, not parallel.

3rd: Why the condition is the current ob level greater than the default level +1?

I think that has been answered. when ob_get_level() > $this->_ci_ob_level + 1 then we are trying to call load->view() again in the view that is currently loading.

4th: If a view is flushed manually here, would it still be adjusted by Output->_display()?

Where is "here"? But I think the answer is no.

Upvotes: 1

Related Questions