views:

55

answers:

4

Hello all,

im trying to ding into codeigniter MVC but i dont know how i make my template so i have 2 files (header and footer) and then i can make my controllers and then ONLY put information in the "content" div, so i include the top and the header a good way like this

<?php
include("header.php");
?>
<div id="content">My content here</div>

<?php
include("footer.php");
?>

hope you understand what i mean and can help me out :)

A: 

Mmmm, no not really. Here's how you should be doing MVC.

Inside your controller, have something like this:

$this->load->view('header');
$this->load->view('content');
$this->load->view('footer');

Finally, create views for each of these files (inside the /system/application/views folder)

Henri Watson
+3  A: 

The best way is to load views within your views.

Within views/content.php:

<? $this->view('header', array('title'=>'The page title', 'keywords'=>'some keywords', 'etc'=>'...')); ?>
<div id="content">My content here</div>
<? $this->view('footer'); ?>

So in your controller you would do this:

$this->load->view('content', $data);

$data could contain 'title' or 'keywords' and that would be implemented as such in your controller:

$data['title'] = 'title';
$data['keywords' = 'keywords';

And this in your 'content' view:

<? $this->view('header', array('title'=>$title, 'keywords'=>$keywords)); ?>
<div id="content">My content here</div>
<? $this->view('footer'); ?>

This question is worded differently, but nearly identical to this one in substance: http://stackoverflow.com/questions/765845/codeigniter-or-php-equivalent-of-rails-partials-and-templates

sholsinger
Good answer, but shouldn't it be `<? $this->load->view('footer'); ?>` instead of `<? $this->view('footer'); ?>`?
captaintokyo
Nope. Since $this no longer refers to the Controller you can just use `<? $this->view('name'); ?>` However, you can still use `<? $this->load->view('name'); ?>` I don't recall specifically what $this is in the view, but I'll look into it. Feel free to chime in if you know.
sholsinger
So, when you're in a view, `$this` is `CI_Loader` which is what `$this->load` refers to in the Controller. So it is perfectly fine to use `$this->view()` to load another view (while in a view).
sholsinger
According to EllisLabs it is more appropriate to do the following in your controller:`$this->load->view('header', $data); $this->load->view('content'); $this->load->view('footer');`Since the `CI_Loader` class caches the extracted `$data` property, anything you pass to the first `view()` call will be available in the following `view()` calls.
sholsinger
A: 

Try using a template library, they will help you tremendously down the road. Helps you manage templates better than default $this->load->view('content', $data);

Here are some great examples:

There are more online, try googling them :)

tpae
+1  A: 

Here is what I do. I have a file called layout.php, with all the html layout in it and inside my , I do:

$this->load->view($template);

In your controller, you can do this:

$data['template'] = 'filename'; 
$this->load->view('layout', $data);

Like this it will load the file called "filename" inside the <div></div>

abdelm