forked from bcit-ci/CodeIgniter
-
Notifications
You must be signed in to change notification settings - Fork 0
codeigniter template
Derek Jones edited this page Jul 5, 2012
·
12 revisions
This tutorial is based on Phil Sturgeon's template and themeing library which can be found at:
http://bitbucket.org/philsturgeon/codeigniter-template/
After downloading the codeigniter-template package from BitBucket, unpack it to the correct library and config directory locations.
- Open up application/config/autoload.php and add 'template' to the libraries array htat are being autoloaded. For example:
$autoload['libraries'] = array('database', 'template');
- Create the directory applications/views/base/ and add a layout.php file including a basic html layout such as:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head>
<?php echo $template['partials']['header']; ?>
</head>
<body>
<?php echo $template['body']; ?>
</body>
</html>
- Create the directory applications/views/basic/partials and add a header.php file in there including your css and metadata stuff, for example:
<title><?php echo $template['title'];?> | Example Site </title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
- Now in some global code (this could be a post_controller_constructor Hook or MY_Controller) add to the constructor the template configuration, for example:
function __construct()
{
parent::Controller();
$this->template->set_layout('layout');
$this->template->enable_parser(FALSE); // default true
$this->template->set_partial('header', 'partials/header', FALSE);
}
- in your controller, if you are using MY_Controller you should extend MY_Controller not Controller. For the index function you should use this code to display your view:
$this->template->build('body', $data);
where index is the view file application/views/index.php or if you are currently using a modular system like Modular Separation it will look in application/modules/modulename/views/index.php.
- the reference to the 'body' in the build() method above represents the view called body.php inside the module's views/ directory and so you should create it and add some content there
Liran Tal liran.tal@gmail.com For any updates, improvements and bugs