Matt Saunders
Matt Saunders

Reputation: 4381

MVC includes/partials (Codeigniter)

I've got an issue at the moment where I have two methods and two views. Each method passes it's Db data to a view, as expected with the MVC pattern.

My issue arises because I want one of the views to be a partial, which is included in the other view.

So it looks something like this:

I want the second view to be it's own unique file, how can I include this in the first form, at any location in the file?

** had to edit as I got the first bit wrong. There is only one controller, with two methods passing data to two views.

Upvotes: 0

Views: 123

Answers (2)

mseo
mseo

Reputation: 3911

I don't know if I understand you correctly, but CodeIgniter has the ability to return a view as data. In the Controller it would look like this:

$data = array();
$data['partial'] = $this->load->view('partial_view', $data, true); // the 3rd parameter let's the view return as data
$this->load->view('whole_view', $data);

The whole_view file would look something like this:

<html>
<head></head>
<body>
<h1>View 1</h1>
<?php echo $partial; ?>
</body>
</html>

The logic, that loads the data for the partial_view could be extracted to a model or so.

Hope this helps!

Upvotes: 1

Erman Belegu
Erman Belegu

Reputation: 4079

You can include the load like this:

view_one.php

<html>
<head></head>
<body>
<h1>View 1</h1>
<?php echo $this->load->view('view_two'); ?>
</body>
</html>

Upvotes: 0

Related Questions