Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load view into a variable

Is there any way that I can get content of a PHP file in to variable?

I want to do this

$msg = $this->load->view('some_view'); 

but when I do this, $msg is NULL.

Is it possible?

like image 957
Sml004 Avatar asked Aug 02 '12 07:08

Sml004


People also ask

How do you load a view in CI?

To load (and display) a view in CodeIgniter, we use the built in Loader library. $this ->load->view( 'hello_world' , $data , true/false); This single line of code will tell CodeIgniter to look for hello_world. php in the application/views folder, and display the contents of the file in the browser.

How to Pass data from controller to view in PHP?

Since a controller writes either to view or model - you'd pass variables to view via controller. $model = new Model(); $view = new View($model); $controller = new Controller($view); // This will assign variables to view $controller->indexAction(); echo $view->render();


2 Answers

It is possible:

$msg = $this->load->view('some_view', '', true); 
like image 89
Yan Berk Avatar answered Oct 13 '22 00:10

Yan Berk


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:

$msg = $this->load->view('some_view', '', true); 

Source : http://codeigniter.com/user_guide/general/views.html

like image 31
Madan Sapkota Avatar answered Oct 12 '22 23:10

Madan Sapkota