Welcome Guest, Not a member yet? Register   Sign In
what $string = $this->load->view('myfile', '', true) exactly does?
#1

[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:
Code:
$string = $this->load->view('myfile', '', true);

I am not getting this...

Is the below code correct?
Code:
$data['title'] = "My Real Title";
$data['heading'] = "My Real Heading";
        
$string = $this->load->view('blogview',$data,true);

I am new to codeigniter framework...
Can someone please explain or show me a simple example?

Thank you in advance for your answers!
#2

[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);
#3

[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);

$this->load->view('blogview',$string);


Code:
<html>
<head>
<title></title>
</head>
<body>
<ul>
<li>&lt;?php echo $string;?&gt;</li>
</ul>
&lt;/body&gt;
&lt;/html&gt;

But I am getting the following error:

Quote:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: string

Filename: views/blogview.php

Line Number: 36

Where I am wrong?
#4

[eluser]mddd[/eluser]
Code:
$string = $this->load->view('blogview',$data,true);
$this->load->view('blogview',$string);
With this code you are loading 'blogview' with $data (given your earlier example, that contains a title etc) and saving it to $string. That's fine.
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'.
#5

[eluser]mddd[/eluser]
Note: another way to pass variables to a view is to use $this->load->vars. An example:
Code:
// example 1

$data = array();
$data['title'] = 'My Title';
$data['heading'] = 'My Heading';
$this->load->vars($data);
// now the variables will be available in the view as $title and $heading.
$this->load->view('my_view');

// example 2

$this->load->vars('title', 'My Title');
$this->load->vars('heading', 'My heading');
// same thing. only now you pass 1 variabele at a time, by giving name and value.
$this->load->view('my_view');
No matter how you pass them, variables are available to ALL views you load afterwards!
#6

[eluser]GeorgiosK[/eluser]
Code:
$data = array();
$data['title'] = 'My Title';
$data['heading'] = 'My Heading';
$this->load->vars($data);
// now the variables will be available in the view as $title and $heading.
$this->load->view('my_view');

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?
#7

[eluser]mddd[/eluser]
Quote:In the above case
$this->load->view(‘my_view’,$data);
would work the same, right?
Yes.

Quote:How can the $string variable be used/proccessed/edited afterwards?
Could you show me a simple example?
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.
#8

[eluser]JoostV[/eluser]
A simple example: create an email.

CONTROLLER
Code:
// Set data dynamically
$data['yourname'] = 'John';

// Load mailcontent as string, spiked with dynamic data
$mail_content = $this->load->view('mail', $data, TRUE);

// Send e-mail, using the generated mailcontent
$this->load->library('email');
$this->email->from('[email protected]', 'Your Name');
$this->email->to('[email protected]');
$this->email->subject('Email Test');
$this->email->message($mail_content);
$this->email->send();

VIEW mail.php
Code:
Hello &lt;?php echo $yourname; ?&gt;
Today is &lt;?php echo date('d M Y'); ?&gt;. Just so you know.
Sincerely, me

Other examples: get an invoice as string from a view and store it, create an XML from view and send it as attachment, etc.
#9

[eluser]GeorgiosK[/eluser]
Thank you again for your answers and sorry for the trouble. I understand how can I use it now. Smile




Theme © iAndrew 2016 - Forum software by © MyBB