CodeIgniter Forums

Full Version: $this->load->view() 3rd parameter TRUE cannot retrieve data
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]dark_lord[/eluser]
Hi All,

I would like to create a function that will enable me to convert HTML to PDF.

Currently here is the scenario I have on my project:

I have used a Library/Helper that will handle the loading of the view files.
Libray Snippet
Code:
public function load($page, $data = NULL, $admin = FALSE, $stream = FALSE)
{
    $data['page'] = $page;
        
    $template = $this->get_default_template();
    $this->CI->load->view('templates/' . $template . '/layout/container', $data, $stream);
}

Controller Loading View Snippet
Code:
$this->_template['page']    = 'blog/post';
$html = $this->system_library->load($this->_template['page'], $data, FALSE, TRUE);

try
{
    pdf_create($html, 'filename');
}
catch (Exception $e)
{
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

As you can see, I have place the $stream variable to TRUE which basically would enable the $this->CI->load->view(); to throw the data version of the view. But for some reason it isn't giving me any result/response?

Question:
1. Is using the get_instance() through $this->CI->load->view(); does not support the 3rd parameter?

2. Is there anything that I am missing on my setup?

Any help from you guys is highly appreciate.

Hope you can shed some light as soon as possible.

Many Thanks and In Advance!

WB

El Forum

[eluser]grisha[/eluser]
Quote:"The third optional parameter lets you change the behavior of the function so that it returns data as a string rather than sending it to your browser. This can be useful if you want to process the data in some way. If you set the parameter to true (boolean) it will return data. The default behavior is false, which sends it to your browser. Remember to assign it to a variable if you want the data returned:"

Code:
$this->CI->load->view('templates/' . $template . '/layout/container', $data, $stream);

You need to return your results.

Code:
return $this->CI->load->view('templates/' . $template . '/layout/container', $data, $stream);

El Forum

[eluser]dark_lord[/eluser]
Code:
$html = $this->system_library->load($this->_template['page'], $data, FALSE, TRUE);

Doesn't that automatically returns the data version when the 3rd parameter is enabled?? In which on my example, I already have a $html variable to handle the return string?

I don't think that answers the questions here... Sad Thanks for the help though...

El Forum

[eluser]grisha[/eluser]
If your function "$this->system_library->load" doesn't return anything, how do you expect $html to contain something?
This might help: http://php.net/manual/en/functions.user-defined.php

El Forum

[eluser]dark_lord[/eluser]
I know functions. :-) Thanks for the tip though.

But isn't it that in codeigniter when you populate the 3rd parameter to be "TRUE" it will return a data version of the view.

For Example:
Code:
$data['header'] = $this->load->view('header', null, TRUE);
$data['content'] = $this->load->view('content', null, TRUE);
$data['footer'] = $this->load->view('footer', null, TRUE);

$this->load->view('container', $data);

So as you can see in the above code the $this->load->view for the header, content and footer has a 3rd parameter which returns the particular file to string/data something like and then pass it to header and then on the container view you can call these array keys...

Returning views as data

There is a third optional parameter lets you change the behavior of the function so that it returns data as a string rather than sending it to your browser. This can be useful if you want to process the data in some way. If you set the parameter to true (boolean) it will return data. The default behavior is false, which sends it to your browser. Remember to assign it to a variable if you want the data returned:

Code:
$string = $this->load->view('myfile', '', true);

So in my scenario like in the above I am using like a Library that will help me on my templating system but the problem is I can't utilize the 3rd parameter and it does not return any value.

El Forum

[eluser]CroNiX[/eluser]
Code:
public function load($page, $data = NULL, $admin = FALSE, $stream = FALSE)
{
    $data['page'] = $page;
        
    $template = $this->get_default_template();
    if($stream)
    {
        //return the captured output as a variable
        return $this->CI->load->view('templates/' . $template . '/layout/container', $data, TRUE);
    }
    else
    {
        //directly output the view to the browser
        $this->CI->load->view('templates/' . $template . '/layout/container', $data);
    }
}

El Forum

[eluser]CroNiX[/eluser]
[quote author="WishBear*" date="1298853385"]
Returning views as data

There is a third optional parameter lets you change the behavior of the function so that it returns data as a string rather than sending it to your browser. This can be useful if you want to process the data in some way. If you set the parameter to true (boolean) it will return data. The default behavior is false, which sends it to your browser. Remember to assign it to a variable if you want the data returned:

Code:
$string = $this->load->view('myfile', '', true);

So in my scenario like in the above I am using like a Library that will help me on my templating system but the problem is I can't utilize the 3rd parameter and it does not return any value.[/quote]

Yes it returns it, but you are not capturing the result and returning it from your load function, so its going nowhere.

El Forum

[eluser]InsiteFX[/eluser]
If it is in a library then you need to get the CI Superobject!

Code:
private $CI;

// in your Constructor - Set the CodeIgniter super object to a local variable for use later.
$this->CI =& get_instance();

// then for your views:
$data['header'] = $this->CI->load->view('header', null, TRUE);

InsiteFX

El Forum

[eluser]dark_lord[/eluser]
[RESOLVED] Thank you all for the help! Y'all ROCKS! \m/.