Welcome Guest, Not a member yet? Register   Sign In
CI 1.7 output buffer broken (for Controller->_output)
#1

[eluser]mycroes[/eluser]
Somehow only views are buffered, content echo-ed is not buffered. Test code:

Code:
class Test extends Controller {

  function Test() {
    parent::Controller();
  }

  function index() {
    echo 'index function';
  }
  
  function _output($output) {
    echo 'test';
  }
}

Using $this->load->view() will work, as long as you don't use echo $this->load->view($param1, $param2, true);

Of course everything can be done just by loading views, but I still have some dirty code that uses more than just views...
Regards,

Michael
#2

[eluser]Pascal Kriete[/eluser]
CodeIgniter does not use the native PHP output buffer. All view contents are stored in a variable instead. This variable is passed to the _output function.

You can easily add your own output to the CI buffer though:
Code:
ob_start();
echo 'a';
echo 'b';
echo 'c';

$buffer = ob_get_contents();
ob_end_clean();

$this->output->append_output($buffer);

// OR
$o = 'a';
$o .= 'b';
$o .= 'c';

$this->ouput->append_output($o);

Hope that helps.
#3

[eluser]mycroes[/eluser]
Why isn't codeigniter using a native php output buffer, or any real output buffer? Seems to me that a function called _output with $output as parameter should receive all output... Also from this I would understand that caching wouldn't work when echoing, and if it does why isn't the same data that's written to cache passed as parameter to the _output function? I'd like some confirmation that current behaviour is as expected...
Regards,

Michael
#4

[eluser]Pascal Kriete[/eluser]
I can't tell you why they do it this way, but I do know that I've used it to my advantage multiple times. Particularly for downloads and async requests.

The caching documentation is pretty clear on this though:
Quote:Warning: Because of the way CodeIgniter stores content for output, caching will only work if you are generating display for your controller with a view.
#5

[eluser]mycroes[/eluser]
I'm currently converting all my pages to get rid of the echos, some pages however don't use views at all right now (written in a few hours, work, so left untouched after that). If anyone else runs into this problem, it's always possible to have a view that just echoes content and then repalce all your echoes with view-loading...
Regards,

Michael
#6

[eluser]beemr[/eluser]
I wrote a quick function for bypassing the view but still getting into the output.

Code:
function bypass_view($rushtml)
    {
        ob_start();
        echo html_entity_decode($rushtml);
        // PHP 4 requires that we use a global
        global $OUT;
        $OUT->append_output(ob_get_contents());
        @ob_end_clean();
    }

Just fill $rushtml with your entity-encoded html;
#7

[eluser]Pascal Kriete[/eluser]
@Beemr, why are you buffering the output for one echo?

Code:
// PHP 4 requires that we use a global
global $OUT;
$OUT->append_output( html_entity_decode($rushtml) );
#8

[eluser]beemr[/eluser]
Honestly, I can't remember :roll: , but I think that I was considering the Template_Inheritance_Helper at the time, which made use of the output buffer to create Django-style templates. It was a neat helper, but I ultimately decided it was overkill versus the just-then-released multiple view loader.

You are right, the ob is unnecessary here.
#9

[eluser]beemr[/eluser]
Sure enough. The function should be:
Code:
function bypass_view($rushtml)
    {
        // PHP 4 requires that we use a global
        global $OUT;
        $OUT->append_output(html_entity_decode($rushtml));
    }

Thanks for the optimization, Inparo
#10

[eluser]mycroes[/eluser]
I could wrap that up into my layout controller, but I still think the CodeIgniter behaviour is wrong... And I'm still waiting for a CodeIgniter dev to answer if this is indeed expected behaviour and if it will always be this way (I for one can understand that people also want to have pages they 'echo' cached...).




Theme © iAndrew 2016 - Forum software by © MyBB