CodeIgniter Forums
Load view into a variable? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Load view into a variable? (/thread-64537.html)



Load view into a variable? - dysko - 03-01-2016

I want to load view into a variable but also want to pass some dynamic data to the view? 
please help

This code works:
$result["view"] = $this->load->view('template/test', '', TRUE);

This returns output directly to the browser:
$result["view"] = $this->load->view('template/test', array("something"=>"something"), TRUE);

https://codeigniter.com/user_guide/general/views.html


RE: Load view into a variable? - albertleao - 03-01-2016

Can you load your snippet of code?


RE: Load view into a variable? - pizzel - 03-01-2016

hi just create an an array called 'anything' like this
$page_data['myvariable']='something'; // the myvariable(or whatever you write) will become a variable once passed to the view, then in the second parameter pass it there like this
$result["view"] = $this->load->view('template/test', '$page_data'); // here you only pass the array name not the whole "$page_data['myvariable']" remember it dosen't have to be called myvariable it can be anything you like also page_data can be anything you like.


RE: Load view into a variable? - PaulD - 03-05-2016

Hi,

You are correctly using the 3rd term set to true in the load view function. This returns the view as a string for your variable.

PHP Code:
$result["view"] = $this->load->view('template/test'''TRUE); 

The second variable which you have left as an empty string is where you would pass the data to the partial view.

PHP Code:
$page_data = array('name' => 'mydata');
$result["view"] = $this->load->view('template/test'$page_dataTRUE); 

You can then access any of the array keys in your partial view as you would normally using, in the example above,

PHP Code:
My name is <?php echo $name?>

Hope that helps,

Paul.


RE: Load view into a variable? - Kailas Bedarkar - 05-28-2018

(03-05-2016, 03:25 AM)PaulD Wrote: Hi,

You are correctly using the 3rd term set to true in the load view function. This returns the view as a string for your variable.

PHP Code:
$result["view"] = $this->load->view('template/test'''TRUE); 

The second variable which you have left as an empty string is where you would pass the data to the partial view.

PHP Code:
$page_data = array('name' => 'mydata');
$result["view"] = $this->load->view('template/test'$page_dataTRUE); 

You can then access any of the array keys in your partial view as you would normally using, in the example above,

PHP Code:
My name is <?php echo $name?>

Hope that helps,

Paul.


Hi Paul,

can I perform jquery operation in test and set the value in view that value is not returned in $result["view"]

view Code

My Name is <?php echo $name ?>
<div id="address"></div>
<script>
$(function(){
    $('#address').html('this is my name print');
});
</script>

The $result["view"] will have contains the address div value. but I'm getting an empty value. 

Can you guide me on that?