在Codeigniter中的库中加载视图页面

In my codeigniter i created a library in library folder.I want to load view pages in that library.How can i do this?

This is my code:

$this->load->view('view_page');

But when iam using this code i get an error:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: CI_theme_lib::$load

Filename: libraries/theme_lib.php

Line Number: 9

What is the problem in mycode?

In Line number 9 in library the code is :

$this->load->view('view_page');

You simply DON'T load pages (aka Views) in a Library. I don't see any need for doing this.

To do what you're trying to do you need to get an instance of CI, and use that.

e.g.

$CI =& get_instance();

Within your library functions you could then use this variable to load the view:

$CI->load->view('view_page'); 

I would question though why you want to call a view, in the form that you have done, within a library. I suspect that you would be better to get the view call to return data (setting the 3rd parameter 'true') and then have your library return the display data to the controller.... Your approach seems messy, but then I have no idea what your library is trying to do.....

I have came across your question for different reason, I seem to have problem passing variables to views instead. Let me explain before I tell you answer to your problem.

Imagine you have an Emailer library to send emails rather than sorting that out in controller. Emailer than within itself builds email string using views. My problem is that when I make my call from controller something like Emailer::send_mail($data,$template) it passes the variables correctly but when I do it from another library the view fails to register the variables. LOL

So yes Stéphane Bourzeix you do sometimes want to use output from view in a different way than just returning to client browser.

The solution is here. https://www.codeigniter.com/userguide2/general/views.html

the last section of that page has something like

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

but something like

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

should work too

in case of doing this from other places than controllers you will need to:

$this->ci = & get_instance();
$string = $this->ci->load->view("myfile",$view_data,true);

it seems like the last argument in the list (true) is the one that tells it not to render to browser but instead just create string with template content

I know it's a bit too late but hope it still helps to some. Good luck with your code.

tomhre