Why do we use TRUE to load a view in CodeIgniter

Controller:

$data = array(); $page['left_content'] = $this->load->view('left_content', $data, TRUE); $page['main_content'] = $this->load->view('left_content', $data, TRUE); $page['right_content'] = $this->load->view('left_content', $data, TRUE); $this->load->view('home',$data); 

View:

 <body> <?php if(isset($left_content)){echo $left_content;}?> <?php if(isset($main_content)){echo $main_content;}?> <?php if(isset($right_content)){echo $right_content;}?> </body> 

Pay attention to the code above. This code is used to view the page on the main page. Now just pass the test. If we just remove TRUE from the code, the code does not work properly. This means that when we delete it, the view does not print itself in the right place. it prints itself at the top of the main window or main home page. I have a lot of googled, but can't find a reason to use it. I just want to know why we just use TRUE in this code? Thnx

+6
source share
3 answers

When we pass TRUE as an optional parameter during the loading of the view, it returns the contents, rather than passing (displaying) the data to the browser directly.

You can check the documentation :

There is a third optional parameter that allows you to 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 value is false, which sends it to your browser. Remember to assign it to a variable if you want to return data:

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

+10
source
0
source

The purpose of the third optional parameter allows you to change the behavior of the function so that it returns data as a string, rather than sending them to your browser. this is very 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 value is false, which sends it to your browser.

Example:

 $dataString = $this->load->view('viewFileName','', true); 

NOTE. Remember to assign it to a variable if you want the returned data

Here the $ dataString variable contains data as a string of the requested view file Read here for more details

0
source

Source: https://habr.com/ru/post/944968/


All Articles