Welcome Guest, Not a member yet? Register   Sign In
include files
#1

[eluser]Avril[/eluser]
Hello,

I want to create seperate files for for example a sidebar, footer, header etc ...
So in plain PHP I create the seperate files and include them in the in an index.php file for example.

How can I do this with CI?

I know that I can use the
Code:
$this->load->file()
function, but since it's a view file it can't work.

I'm sure there's a logic and simple way to do it with CI, but I didn't find it yet Sad
#2

[eluser]bretticus[/eluser]
Load multiple views in your controller for each section of your overall markup.

Code:
class Windoze extends Controller {

function index() {
// ... other code ...
$this->load->view('header', $data);
$this->load->view('sidebar', $data);
$this->load->view('index', $data);
$this->load->view('footer', $data);
}

}
#3

[eluser]Avril[/eluser]
Lol ok, this was easy Big Grin, thanks!
#4

[eluser]jonny_mike_13[/eluser]
thanks ! this imformation is useful
#5

[eluser]bretticus[/eluser]
I guess since there are two people taking this advice now, I better be thorough...

You can avoid passing your data array to each view by using load->vars and all variables with be available to all views...

Code:
class Windoze extends Controller {

function index() {
// ... other code ...
$this->load->vars($data)
$this->load->view('header');
$this->load->view('sidebar');
$this->load->view('index');
$this->load->view('footer');
}

}

One other method for doing this is to load one view, but the view file itself calls other views...

Code:
class Windoze extends Controller {

function index() {
// ... other code ...
$this->load->view('index', $data);
}

}

index.php view...
Code:
<html>
<?php $this->load->view('header', $data);?>

<?php $this->load->view('sidebar', $data);?>

<?=$content?>

<?php $this->load->view('sidebar', $data);?>
</html>
#6

[eluser]nuwanda[/eluser]
Jeff Way's screencasts on Nettuts are great.

I do this in my controllers:

Code:
$this->data['content']='user/login_form_view';
$this->load->vars($this->data);
$this->load->view('template');

Then in the template which is just a normal view:

Code:
$this->load->view('common/header');
$this->load->view($content);
$this->load->view('common/footer');

So, for every page the header and footer are the same (common). But the content is dynamic according to
the needs of the controller.

You can cascade this on and on since views can be loaded from views and all of the data set in the controller is available to all views. There's no need for any complex templating.

For instance, you could reference a sidebar in your controller and then display it in your content view.

All of the data ($this->data) that gets loaded is available to all views.




Theme © iAndrew 2016 - Forum software by © MyBB