Jump to content

ob_flush()


Destramic

Recommended Posts

hey guys...im having a issue when using ob_flush()...what im getting is a 1 at the end of my html depending on how many templates i render...in this instance i get 11 at the end of my html in the browser,

 

render in my view class:

	public function render($file_name)
	{	
		if (preg_match("/\.html$/i", $file_name))
		{
		    ob_start();
			extract($this->_variables);
			require_once PRIVATE_DIRECTORY . 'application' . DS . 'views' . DS . $file_name;
		    $body = ob_flush();
		}
 
	   $this->_repsonse->append_body($body);
	} 

inside my controller:

$this->view->render('template.html');

template.html

my header is here

<? $this->view->render('content.html') ?>

my footer here

how the source looks in browser:

my header is here

content is here

my footer here11

any idea why i get 1's after my html when using ob_start() please?

 

thank you

Link to comment
https://forums.phpfreaks.com/topic/296724-ob_flush/
Share on other sites

Have you tried commenting out the output buffer block of code to verify that's what's causing the issue? For example, you could try something like this:

public function render($file_name)
{ 
    /*
    if (preg_match("/\.html$/i", $file_name))
    {
        ob_start();
        extract($this->_variables);
        require_once PRIVATE_DIRECTORY . 'application' . DS . 'views' . DS . $file_name;
        $body = ob_flush();
    }
    */
 
    $body = "<div>All good here ($file_name)</div>";
    $this->_repsonse->append_body($body);
}
 
Perhaps the issue is caused by append_body()...
Link to comment
https://forums.phpfreaks.com/topic/296724-ob_flush/#findComment-1513546
Share on other sites

Well, I assume that the following line of code would display content on the screen without buffering the output:

require_once PRIVATE_DIRECTORY . 'application' . DS . 'views' . DS . $file_name;

Instead of displaying it directly to the screen, it looks like you want to assign the content to $body. If that's correct, you would use ob_get_contents(). Note that this function doesn't clear the buffer, so you'll need to also call ob_end_clean().

 

You could try changing this

public function render($file_name)
{ 
    if (preg_match("/\.html$/i", $file_name))
    {
        ob_start();
        extract($this->_variables);
        require_once PRIVATE_DIRECTORY . 'application' . DS . 'views' . DS . $file_name;
        $body = ob_flush();
    }
 
    $this->_repsonse->append_body($body);
}

To this

public function render($file_name)
{ 
    if (preg_match("/\.html$/i", $file_name))
    {
        ob_start();
        extract($this->_variables);
        require_once PRIVATE_DIRECTORY . 'application' . DS . 'views' . DS . $file_name;
        $body = ob_get_contents();
        ob_end_clean();
    }
 
    $this->_repsonse->append_body($body);
}
Link to comment
https://forums.phpfreaks.com/topic/296724-ob_flush/#findComment-1513565
Share on other sites

Typo?

 

$this->_repsonse->append_body($body);

$this->_response->append_body($body);

 

thanks for that :)

 

 

 

ob_flush(), contrary to the manual, appears to return true (1)

ob_start();
echo "";
$body = ob_flush();
var_dump($body);      //--> bool(true)
		if (preg_match("/\.html$/i", $file_name))
		{
		    ob_start();
			extract($this->_variables);
			require_once PRIVATE_DIRECTORY . 'application' . DS . 'views' . DS . $file_name;
			$body = ob_get_contents();
		    ob_clean();
		}
 
	   $this->_response->append_body($body);

solved it now...thanks for your input guys :)...much appreciated

Link to comment
https://forums.phpfreaks.com/topic/296724-ob_flush/#findComment-1513698
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.