Most Simple Template Library for CodeIgniter

Note: Updated on November 1, 2008. Previous version is still available.

Example

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('template', 'about', $data);

That loads the view about.php into template.php.

Installation

  • Put Template.php into system/application/libraries
  • Autoload it in config/autoload.php:
$autoload['libraries'] = array('template');

How to use it

Create a template file

views/template.php

<html>
<body>
    <div id="contents"><?= $contents ?></div>
    <div id="footer">Copyright 2008</div>
</body>
</html>

$contents is where your view will be inserted.

Create a view

views/about.php

<h1>About</h1>
<p>I'm so human!</p>

Load the view into the template

In your controller:

$this->template->load('template', 'about');

How this library works

The view is loaded into a variable which is given to the template.

var $template_data = array();
 
function set($name, $value)
{
	$this->template_data[$name] = $value;
}
 
function load($template = '', $view = '' , $view_data = array(), $return = FALSE)
{               
	$this->CI =& get_instance();
	$this->set('contents', $this->CI->load->view($view, $view_data, TRUE));			
	return $this->CI->load->view($template, $this->template_data, $return);
}

Advanced use 1: more slots in your template

Example: you want to set the page title.

Add an HTML header to views/template.php

<head>
    <title><?= $title ?></title>
</head>

Set the title in your controller before loading the view:

$this->template->set('title', 'About me');

Advanced use 2: highlight the current navigation item

Navigation is usually part of the template. However, it's a good practice to highlight the item corresponding to the current page, so your visitors know where they are.

Define your navigation items

Add to libraries/Template.php under $this->set('contents...:

$this->set('nav_list', array('Home', 'Photos', 'About', 'Contact'));

Update your template

Add in views/template.php:

<ul class="navigation">
	<?php foreach($nav_list  as $i => $nav_item): ?>
		<li class="<?= ($nav == $nav_item ? 'selected' : '')?>">
			<?= anchor($nav_item, $nav_item) ?>
		</li>
	<?php endforeach ?>
</ul>

To be able to use the anchor function, autoload the url helper in config/autoload.php:

$autoload['helper'] = array('url');

Update your controller

Add:

$this->template->set('nav', 'About');

Notes:

  • if all methods of your controller are under the same nav item, you can set the nav directly in the constructor.
  • to actually highlight the current item, define the corresponding CSS (for #navigation .selected).

Advanced use 3: multiple templates

To easily deal with multiple templates, define new load methods in libraries/Template.php instead of modifying the existing one. Advanced use 2 with a custom method:

function load_main($view = '', $view_data = array(), $return = FALSE)
{
	$this->set('nav_list', array('Home', 'Photos', 'About', 'Contact'));
	$this->load('template', $view, $view_data, $return);
}

Corresponding code in your controller:

$this->template->set('nav', 'About');
$this->template->set('title', 'About me');
$this->template->load_main('about');

Contributors

  • Jram: fix to use views in subdirectories
  • Moshe Teutsch: view loading with CI method instead of include_once. That makes things much cleaner and simpler!
 

Suzi nowhere usa
November 21, 2008

I know i'm being dense, but how would you change the nav_list items anchor build? Say you want the menu item in the nav list to say Photos but the link itself for navigation purposes needs to point to a controller that may not be named photos?

It will build like
http://www.someplace.com/ci-app/photos
but i really need
http://www.someplace.com/ci-app/gallery/add

 

Jérôme Jaglale Vancouver
November 21, 2008

You're not being dense at all.

In libraries/Template.php, set nav_list like that:

$this->set('nav_list', array('photos' => 'Photos', '/gallery/add' => 'Add Photo'));

Then in views/template.php:

<?php foreach($nav_list  as $id => $nav_item): ?>
	<li class="<?= ($nav == $id ? 'selected' : '')?>">
		<?= anchor($id, $nav_item) ?>
	</li>
<?php endforeach ?>

 

Suzi nowhere usa
November 21, 2008

That works perfect and actually makes sense to me now! Thanks for the help and making the library available. It really is the most simple template library for CI!
 
write a message
Name


City


Email (won't be displayed)




Antispam: enter the current year (2008)