Overview

CodeIgniter-Template

CodeIgniter-Template is a Template library that helps your build complex views with CodeIgniter. It has logic to work with themes & modules and helps add your title, meta-data, breadcrumbs and partial views.

Usage

For any of this to work well, you need some global code running. You can do this by creating a hook or use MY_Controller. I take MY_Controller one step further and have Public_Controller and Admin_Controller, from which all of my controllers inherit shared logic for the frontend or the backend.

Global methods

Wrap application/views/layout_name.php around your view

$this->template->set_layout('layout_name');

Use Parser for the layout and loaded views

$this->template->enable_parser(FALSE); // default = TRUE

Set a theme name and your layout will be loaded from application/themes/theme_name/views/layouts/layout_name.php

$this->template->set_theme('theme_name');
$this->template->set_layout('layout_name');

Per-page methods

Load and output view (from the current module, or from the view folder if you aren't using a module

$this->template->build('view_name', $data);

Return the built template

$output = $this->template->build('view_name', $data, TRUE);

Set layout and build template

$this->template->set_layout('layout_name'); // Should be in application/views/layout_name.php
$this->template->build('view_name', $data);

Set page title manually (Args, CSV or Array). The default is "Module | Controller | Method".

$this->template->title('Some title', 'Extra part')->build('view_name', $data); // Args
$this->template->title('"Some title","Extra part"')->build('view_name', $data); // CSV
$this->template->title(array('Some title', 'Extra part'))->build('view_name', $data); // Array

Set partial view (assigns a view to $template['partials']['partial_name']). It will use the same logic to find a view partial as build(), in that it will try and find it in modules and themes or just use global view folder.

$this->template->set_partial('sidebar', 'partials/sidebar_view');

// Don't search, use exactly what was provided
$this->template->set_partial('partial_name', 'partials/view_file', FALSE); // application/views/partials/view_file.php

Set breadcrumbs

$this->template->set_breadcrumb('Blog', 'blog/index')
    ->set_breadcrumb($article->category->title, 'blog/category/'.$article->category->id)
    ->set_breadcrumb($article->title, 'blog/article/'.$article->id);

Add lines of metadata

// <meta name="keywords" content="some, keywords, somebody, for, to, comma-separate" />
$this->template->set_metadata('keywords', 'some keywords somebody for to comma-separate');
$this->template->set_metadata('keywords', 'some, keywords, somebody, for, to, comma-separate');
$this->template->set_metadata('keywords', array('some', 'keywords', 'somebody', 'for', 'to', 'comma-separate'));

// <link rel="canonical" href="http://example.com/some/other/page" />
$this->layout->set_metadata('canonical', site_url('some/other/page'), 'link');

// <base href="http://example.com/some/base/url/" />
$this->layout->set_metadata('', 'http://example.com/some/base/url/', 'base');

// <link rel="stylesheet" href="/assets/css/style.css" type="text/css" charset="utf-8" />
$this->layout->set_metadata('stylesheet', '/assets/css/style.css', 'link', 'charset="utf-8"');

Pretty much all of the methods support method-chaining in PHP 5 to make syntax shorter.

Installation

Drop the libraries/Template.php file into your applicaiton/libraries folder.

Requirements

Only CodeIgniter

Extra

If you'd like to request changes or report a big, please use the issue tracker.