Welcome Guest, Not a member yet? Register   Sign In
Loading view from static method?
#1

[eluser]gRoberts[/eluser]
Hi all,

I'm wondering whether it's something i'm doing wrong or not but the following code just produces a white page.

Code:
public static function TimedRedirect($Title, $Content, $Url, $Timeout = 5, $CI) {

            //$CI =& get_instance();
            $data = array();
            $data['page_title'] = $Title;
            $data['page_css'] = implode("\n", array(
                '<link rel="stylesheet" type="text/css" media="screen" href="/assets/css/box.css" />'    
            ));
            $data['page_javascript'] = implode("\n", array());
            $data['page_other'] = implode("\n", array(
                '<meta http-equiv="refresh" content="'.$Timeout.';url='.$Url.'" />'
            ));
            $data['Content'] = $Content;        
            $data['Url'] = $Url;
            $data['Timeout'] = $Timeout;
            $CI->load->view('common/site_box', $data);
            exit();

        }

As you can see I have commented out the get_instance call as that gave the same result (the example above passes the CI object by reference using $this).

I need to be able to quickly display a generic view (this is before any other views are loaded) but it seems that CI doesn't like what i'm doing, even though it looks as if it should work.

Has anyone got any idea's?

Cheers

Gavin
#2

[eluser]Rick Jolly[/eluser]
2 things.

1) If you want to pass the CI instance into your function, you need the amperstand:
Code:
public static function TimedRedirect($Title, $Content, $Url, $Timeout = 5, & $CI)
2) I think due to CI's output buffering, you can't use exit() at the end of your function without outputting the buffer first. Maybe try this:
Code:
ob_start();
$CI->load->view('common/site_box', $data);
ob_end_flush();
exit();
#3

[eluser]gRoberts[/eluser]
Hi Rick,

Thank you, but unfortunately ob_end_flush threw an error,

Code:
An error has occurred!

Array
(
    [ErrorNo] => 8
    [Message] => ob_end_flush() [ref.outcontrol]: failed to delete and flush buffer. No buffer to delete or flush.
    [File] => /home/stevecs/domains/collectionstation.com/public_html/test/system/application/libraries/Classes/Core.php
    [Line] => 41
)

When I commented out that line, back to the same situation.

Thanks

Gav
#4

[eluser]Pascal Kriete[/eluser]
$this->load->view adds the output to the CI output buffer, if you exit it won't output anything.

The easiest thing to do here is get the text output, echo it, and then exit:
Code:
$out = $CI->load->view('common/site_box', $data, TRUE);
echo $out;
exit();




Theme © iAndrew 2016 - Forum software by © MyBB