Codeigniter, how to make a template
im trying to 开发者_如何学Pythonding 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 :)
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
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>
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:
Most Simple Template Library for CodeIgniter
Yet Another Codeigniter Template System
There are more online, try googling them :)
精彩评论