Codeigniter and the big picture
Have recently started using Codeigniter and have gotten through most of the tutorials. I'd like to convert a smallish PHP/MySQL website to run entirely on the CI framework. The code is four years old, written in PHP4, and is a mess. I don't mind rewriting all of it.
The problem I'm having is find the 'right' solution to this problem. I'd like to keep the code as OOP/PHP5 as possible, and as clean as possible as well. The website has five six main components arranged in to DIVs. Two stack to form a center column, one left, one right, one header/menu, and one dynamically filled Fancybox. The current index.php simply calls header.php, then goes on to <div class="class1"><include ('box1.php')</div>
and so on.
The codeigniter examples I've seen use $this->load->view('header')
and footer on each template file to get the structure set up. This seems to be a redundant and messy way to handle the header, loading of the CSS file, etc.
What is the proper way to get these controllers and associated views loaded? The codeignite开发者_运维问答r version of a 'wrapper', if you will.
get a template library, i personally prefer working with either of Colin Williams or Phil Sturgeons.
in essence, you do something like this: (assuming "layout" is your HTML template)
layout.php (view)
<html>
<head>
<title><?=$title;?></title>
<?=$metaData;?>
</head>
<body>
<div id="left">
<?=$leftContent;?>
</div>
<div id="main">
<?=$main;?>
</div>
</body>
</html>
then in your controller:
site.php (controller)
class Site extends Controller {
function __construct()
{
$this->load->library('template');
$this->template->set_layout('layout');
}
function index()
{
$this->template->write_region('title', 'This is my title');
$data['somedata'] = 'foo'
$this->template->write_region('main', 'My content', $data);
$this->template->render();
}
}
This will output your data in to your template, without needing to have load views each time etc.
My syntax is a little off, so check library specific docs for the correct code, but template libraries are the way to go imo.
精彩评论