CodeIgniter Forums

Full Version: Assigning view to a array element in controller
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]onblur[/eluser]
In an effort to keep as much code out of views as possible, I'm trying to assign a view to an array element in the controller:

Code:
$data['pagePart'] = $this->load->view('somePartOfPage');

Then in the view to be rendered:

Code:
<!-- Header Stuff -->
<body>
<!-- Some other output here -->
<?=$pagePart?>
<!-- Rest of page here -->

Now, the element looks like it should except it is in the wrong place - instead of being further down the page, it renders straight after the opening body tag:

Code:
<!-- Header Stuff -->
<body>
<!-- Output from array element is being output here !?! -->
<!-- Some other output here -->
<!-- Rest of page here -->

It doesn't seem to matter where the array element is inserted in the view code, it is always ends up being output immediately after the <body> tag.

What am I missing?

El Forum

[eluser]CI Begby[/eluser]
It should be

Code:
$string = $this->load->view('viewfile', true);

The second boolean argument tells the loader to return the view as a string instead of sending it straight to the browser, which is what is happening to you now.

El Forum

[eluser]onblur[/eluser]
Ahh, yes. Thank you.

Just a small, but crucial, correction - it needs to be the third parameter.

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