Ermir
Ermir

Reputation: 1451

CodeIgniter view loading not loading in order?

I have a Controller in CodeIgniter with the following code:

$this->load->view("first");
echo "LOL";
$this->load->view("second");

But strangely, when the page is rendered, the HTML is:

LOL
<div id="firstView"></div>
<div id="secondView"></div>

I have no idea what could be causing this reordering of the statements. Any suggestions? Might I have overlooked something?

Upvotes: 1

Views: 3240

Answers (4)

Akshay Raje
Akshay Raje

Reputation: 892

Here's a more elegant way to do this:

$this->load->view("first");
$this->output->append_output("LOL");
$this->load->view("second");

Using this will reduce rewrites of pre-written $this->load->view statements into echos. Hope it helps.

Upvotes: 1

ivanargulo
ivanargulo

Reputation: 324

To achieve what you are trying, you have to pass a third param to the function $this->load->view(...), indicating that you'll receive the view in a variable, and not to display it immediatly.

Like this:

echo $this->load->view("first", NULL, TRUE);
echo "LOL";
echo $this->load->view("second", NULL, TRUE);

Look at the third param TRUE (the second is all the variables you want to pass to it). Don't forget to print the result of the view with echo. This is very useful if you want to store the views and process them, or print them in the order you wish.

Upvotes: 3

Sebastien Marion
Sebastien Marion

Reputation: 270

Rendering views in code igniter is done at the end. If you wish to render them as you go you can do

echo $this->load->view("first", array(), true);
echo "LOL";
echo $this->load->view("second", array(), true);

Upvotes: 7

hek2mgl
hek2mgl

Reputation: 158020

It's simple:

$this->load->view("...");

will not immediately display the view. This happens later, what is one of the benefits of a MVC framework.

Upvotes: 3

Related Questions