Simon
Simon

Reputation: 31

Codeigniter, how to make a template

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 :)

Upvotes: 3

Views: 3425

Answers (3)

Abdelrahman Ahmed
Abdelrahman Ahmed

Reputation: 541

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>

Upvotes: 3

tpae
tpae

Reputation: 6346

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 :)

Upvotes: 0

sholsinger
sholsinger

Reputation: 3078

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: CodeIgniter or PHP Equivalent of Rails Partials and Templates

Upvotes: 6

Related Questions