Warning: this tutorial and library are out of date. Prefer the last version.
How you usually load a view:
$this->load->view('about', $data);
How you load a view into a template with this library:
$this->template->load('mainTemplate', 'about', $data);
It loads the view about.php into the template mainTemplate.php.
$autoload['libraries'] = array('template');
views/mainTemplate.php
<html> <head> <meta name="author" content="Who am I?" /> </head> <body> <div id="header"> Cooking by yourself is worth! </div> <div id="contents"> <?php require_once($template_contents.'.php'); ?> </div> <div id="footer"> Copyright maestric.com </div> </body> </html>
The magic comes with this line:
<?php require_once($template_contents.'.php'); ?>
which indicates where your view code will be inserted.
views/about.php
<h1>Template Library for Code Igniter.</h1> <p>Ladies and gentlemen, yes, that's unbelievable!</p>
In a controller's method:
$this->template->load('mainTemplate', 'about');
The source code is incredibly short and straightforward: libraries/Template.php
class Template { function load($template = '', $view = '' , $vars = array(), $return = FALSE) { $vars['template_contents'] = APPPATH . 'views/' . $view; $this->CI =& get_instance(); return $this->CI->load->view($template, $vars, $return); } }
Life is not so simple. You'll probably need more slots in your template, for example for the HTML header's title. To keep the code readable, I write one method for each template: libraries/Template.php
class Template { function load($template = '', $view = '' , $vars = array(), $return = FALSE) { $this->CI =& get_instance(); $vars['template_contents'] = $view; if ($template == "mainTemplate") return $this->loadMainTemplate($view, $vars, $return); else return $this->CI->load->view($template, $vars, $return); } function loadMainTemplate($view, $vars = array(), $return = FALSE) { if( ! isset($vars['head_title'])) $vars['head_title'] = "Default Title"; return $this->CI->load->view("mainTemplate", $vars, $return); } }
In the HTML header of your template file:
<title><?= $head_title ?></title>
Then you can define this title directly in your controllers the normal way:
$data['head_title'] = 'Photos'; $this->template->load('mainTemplate', 'about', $data);
Usually the menu is a fixed part of your pages and should be in your template file. However, you may want to highlight the item corresponding to the current page, so your visitor knows where he is.
Add to Template.php:
var $currentMenuItem = ""; function setCurrentMenuItem($str) { $this->currentMenuItem = $str; }
Add to Template.php in loadMainTemplate():
$vars['menuItems'] = array('Home', 'Photos', 'Software', 'About', 'Contact'); $vars['currentMenuItem'] = $this->currentMenuItem;
Your new Template class:
class Template { function load($template = '', $view = '' , $vars = array(), $return = FALSE) { $this->CI =& get_instance(); $vars['template_contents'] = $view; if ($template == "mainTemplate") return $this->loadMainTemplate($view, $vars, $return); else return $this->CI->load->view($template, $vars, $return); } // -------------------------------------------------------------------- // Main Template var $currentMenuItem = ""; function setCurrentMenuItem($str) { $this->currentMenuItem = $str; } function loadMainTemplate($view, $vars = array(), $return = FALSE) { $vars['menuItems'] = array('Home', 'Photos', 'Software', 'About', 'Contact'); $vars['currentMenuItem'] = $this->currentMenuItem; return $this->CI->load->view("mainTemplate", $vars, $return); } // -------------------------------------------------------------------- }
You can now use these variables in mainTemplate.php:
<html> <body> <div class="menu"> <?php foreach($menuItems as $i => $menuItem): if ($menuItem == $currentMenuItem) $anchorAttribbutes = array('class' => 'selected'); else $anchorAttribbutes = array(); echo anchor($menuItem, $menuItem, $anchorAttribbutes); if (isset($menuItems[$i + 1])) echo " | "; endforeach; ?> </div> <div id="contents"> <?php require_once($template_contents.'.php'); ?> </div> </body> </html>
To be able to use the anchor function, you can autoload the url helper in config/autoload.php.
To choose the current menu item, you can add some lines like that in your controller:
$this->template->setCurrentMenuItem('Photos');
This line can be put in a method, or directly in the controller if all controller's methods share the same menu item. You just need now to define the corresponding CSS style (#menu .selected).