what $string = $this->load->view('myfile', '', true) exactly does? |
[eluser]GeorgiosK[/eluser]
Quote: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: I am not getting this... Is the below code correct? Code: $data['title'] = "My Real Title"; I am new to codeigniter framework... Can someone please explain or show me a simple example? Thank you in advance for your answers!
[eluser]mddd[/eluser]
Yes your code is correct. With your code, you are loading the view (with your title and heading) and saving that in $string. So now you can use $string for whatever you need. Do some processing with it, display it as part of another view, etc. If you wanted to just output the view to the browser you would use $this->load->view('blogview', $data);
[eluser]GeorgiosK[/eluser]
OK then the following is correct and it is supposed to return a string. Code: $string = $this->load->view('blogview',$data,true); Code: <html> But I am getting the following error: Quote: Where I am wrong?
[eluser]mddd[/eluser]
Code: $string = $this->load->view('blogview',$data,true); But than you are loading the view AGAIN. This time passing $string. Why? This would put the total view inside itself! The error is because you need to pass an array to the view. The keys of that array then become variables in the view. So if you pass $data['title'] = 'My Title'; to the view, in the view you would use $title to display the value 'My Title'.
[eluser]mddd[/eluser]
Note: another way to pass variables to a view is to use $this->load->vars. An example: Code: // example 1
[eluser]GeorgiosK[/eluser]
Code: $data = array(); In the above case $this->load->view('my_view',$data); would work the same, right? What confuses me is the third optional parameter. if true the whole view is passed to another variable (in this case $string). How can the $string variable be used/proccessed/edited afterwards? Could you show me a simple example?
[eluser]mddd[/eluser]
Quote:In the above caseYes. Quote:How can the $string variable be used/proccessed/edited afterwards?It allows you to use views with more flexibility. Suppose you want to create an email in html. You can write that email as a view. You load the view, while passing all the personalized information (as variables). Instead of outputting the view directly, you 'catch' it in the string and then send it with the Email library. Or if you have an element that returns multiple times in your webpage. Like a list of restaurants or whatever. Probably a list of div's with all kinds of data inside. You could make a view called 'restaurant-item' and load that same view multiple times, once for each item in the list. Catch the output in an array and send that, along with other information, to a 'main view'. That way, the main view stays shorter and more easy to understand.
[eluser]JoostV[/eluser]
A simple example: create an email. CONTROLLER Code: // Set data dynamically VIEW mail.php Code: Hello <?php echo $yourname; ?> Other examples: get an invoice as string from a view and store it, create an XML from view and send it as attachment, etc.
[eluser]GeorgiosK[/eluser]
Thank you again for your answers and sorry for the trouble. I understand how can I use it now. ![]() |
Welcome Guest, Not a member yet? Register Sign In |