What is the best way to use controllers/views in CodeIgniter?
Is the following a bad way to go about making my pages?
function index()
{
$this->load->library('carabiner');
$this->load->view('include/head');
$this->load->view('include/home');
$this->load->view('top');
$this->load->view('featured');
$this->load->view('eventslist');
$this->load->view('footer');
}
function create()
{
$this->load->view('include/head');
$this->load->view('top');
$this->load->view('page');
$this->load->view('dynamicstuff');
$this->load->view('footer');
}
I’m quasi-templating this way, and it works fine for getting the outline of the site, but is it the best way to create an app?
I’ve been working in CodeIgniter for about a week now, and I’m new to PHP.
There is a download called CodeIgniter Library that makes all this very easy. Once you learn how to implement it, coding becomes very easy. It also allows you to change each page or controller to have different templates (layouts).
Using the system I mentioned above you can do the following:
Anywhere you can also change which template you wish to use:
All these templates have names that are kept in a configuration file. You can have as many templates as you wish.
The template file itself would look something like this:
You can even set up sections to be able to write your content to. I don’t usually do this because it is including a lot of views, but if you need full control like this you are still able to do so:
HTML template code:
This way you only have to worry about including one file in all your Controller Functions. If you are not using PHP 5 (you should switch) then instead of using
<?=$content?>you will want to use<?php echo $content; ?>.