PHP Classes

Simple PHP CodeIgniter Template Engine: Process templates with partial views and widgets

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 85 All time: 10,033 This week: 673Up
Version License PHP version Categories
simple-codeigniter 1.0Custom (specified...5PHP 5, Libraries, Templates
Description 

Author

This class can process templates with partial views and widgets.

It takes as parameter a template file that may contain HTML with PHP like tags that can have PHP commands like echo and variable names inside the tags.

The package can process the templates and generate PHP scripts that when executed they can generate the output by calling classes of the package.

The package classes implement functionality like:

- Partial template manipulation
- Publishing a template by passing a name and an array of parameters
- Built-in triggers that take simple parameters and generate the output for common Web page parts, like external CSS and JavaScript
- Widgets to output common page elements that can be inserted anywhere in a page
- Caching the output of the processing of a template to avoid processing the same template again

Picture of Arumugam P
  Performance   Level  
Name: Arumugam P <contact>
Classes: 5 packages by
Country: India India
Age: 34
All time rank: 3109200 in India India
Week rank: 192 Up22 in India India Up
Innovation award
Innovation award
Nominee: 2x

Winner: 1x

Documentation

CodeIgniter Template Library

This template library for Codeigniter lets you build complex templates using partial views and widgets. It's built with the same method chaining support that we are seeing so often in Codeigniter so it feels familiar. This library loads a template file that uses partial views. These partial view sections are internally represented by Partial Objects managed by the template library. These objects let you modify their content in a user friendly way through method chaining.

Installation

Copy the files to the corresponding folder in your application folder (or use spark).

Configuration

In your template.php config file you can change following configuration parameters (optional):

/*
| -------------------------------------------------------------------
| Template configuration
| -------------------------------------------------------------------
| This file will contain the settings for the template library.
|
| 'parser'     = if you want your main template file to be parsed, set to TRUE
| 'template'   = the filename of the default template file
| 'cache_ttl'  = the time all partials should be cache in seconds, 0 means no global caching
*/

$config['parser']    = FALSE;
$config['template']  = 'template';
$config['cache_ttl'] = 0;

If you prefer, you can autoload the library by adjusting your autoload.php file and add 'template' to the $autoload['libraries'] array.


Template files

Template files are loaded or parsed by Codeigniter and the partials are passed to them as data. You can easily load them like you would normally use data in your view files:

<head>
    <title><?php echo $title; ?></title>
    <?php echo $stylesheet; ?>
</head>
<body>
    <?php echo $content; ?>
</body>

Or when parsing is enabled you can use {content} etc.

However, I prefer to directly call the library's methods from inside the template file to work around php's Undefined variable errors when you are not setting all partials. Calling these methods well replace non-existing partials with empty one's so you don't get any errors:

<head>
    <title><?php echo $this->template->title; ?></title>
    <?php echo $this->template->stylesheet; ?>
</head>
<body>
    <?php echo $this->template->content; ?>
</body>

These variables are in fact Partial Ojects, so you can still manipulate them from inside the template view file like this:

<?php echo $title->prepend('My Website - '); ?>

Partial manipulation methods will always return the partial object itself for further chaining or for displaying. So this is perfectly possible:

<?php echo $sidebar->cache(500)->widget('login')->prepend('Login: '); ?>

Partial manipulation

Partials have a few handy methods for manipulating their content such as:

$partial->set() - overwrites the content
$partial->append() - append something
$partial->add() - same as append (alias)
$partial->prepend() - prepend something
$partial->content() - gets the content
$partial->default() - only set content if empty

You can also load dynamic content inside partials from view files or widgets. The object named partial used in the method chaining below is the name of the partial you want to load the content into.

$this->template->partial->view()

Append or overwrite the partial with a view file with parameters.

$this->template->partial->view('view-file', array(), $overwrite=FALSE);

Append or overwrite the partial with a parsed view file with parameters.

$this->template->partial->parse('view-file', array(), $overwrite=FALSE);

Append or overwrite the partial with a widget's output.

$this->template->partial->widget('widget-name', array(), $overwrite=FALSE);

Publishing

The template class only has a few methods. I chose to do this because almost everything can be managed by using the flexible Partial Object. If you want to publish the entire template with the current partials to the output you can use the publish() method.

You can pass a custom template file and optional data if wanted:

$this->template->publish('template', array('title'=>'Title is overwritten!'));

Most of the time this will be empty using the template file from the config:

$this->template->publish();

If you wish to set the template file before publishing, eg. in a controller's constructor:

$this->template->set_template('template2');

Triggers

Some partials have built in triggers:

stylesheet - you only need to pass the url
javascript - you only need to pass the url
meta - will convert the arguments to the right meta tag
title - converts special characters
description - will convert special characters just like the title

This is an example of what these built in triggers do:

$this->template->stylesheet->add('stylesheet.css', array('media' => 'all'));
//<link rel="stylesheet" href="http://myweb.com/stylesheet.css" media="all" />
 
$this->template->javascript->add('script.js');
//<script src="http://myweb.com/script.js"></script>
 
$this->template->meta->add('robots', 'index,follow');
//<meta name="robots" content="index,follow" />
 
$this->template->title->set('Dad & Son');
//Dad &amp; Son

You can set your own triggers for functions or methods for any partial object like this:

//function
$this->template->partial->bind('strtoupper');
 
//method
$this->template->partial->bind($this->typography, 'auto_typography');

This will trigger the function or method whenever you manipulate the partial's content.

Widget

Widgets are intelligent partial objects. When their content is asked, their display() method is activated which will fill the content using codeigniter or partial object methods. Widgets classes are found inside the application/widgets folder. They extend the main Widget class which has the same methods as the Partial class. This is an example widget:

/File: widgets/hero_widget.php/
class hero_widget extends Widget {
     public function display($args = array()) {
         $this->load->model('my_model');
         $data = $this->my_model->all();
 
         $this->load->view('widgets/hero', $data);
     }
}

And this is loaded from a controller like this:

$this->template->partial->widget('hero_widget', $args = array());


Caching

I did not want to expand the library in all different ways, therefore I implemented a basic caching function using Codeigniter's caching driver. This might slow your code down on simple websites but allows you to use caching for partials just like you would do yourself with Codeigniter's driver.

You can cache particular partials:

$this->template->partial->cache(100);

Or you can cache all partials:

$this->template->cache(100);

Both methods have an extra optional identification parameter that you can use to have multiple cache files for different pages:

$this->template->cache(100, 'frontpage');

  Files folder image Files (20)  
File Role Description
Files folder imageconfig (3 files)
Files folder imagedemo (2 files, 1 directory)
Files folder imagelibraries (1 file)
Plain text file LICENSE Lic. License text
Plain text file README.md Doc. Read me
Plain text file spark.info Data Auxiliary data

  Files folder image Files (20)  /  config  
File Role Description
  Plain text file autoload.php Aux. Auxiliary script
  Plain text file config.php Aux. Auxiliary script
  Plain text file template.php Aux. Auxiliary script

  Files folder image Files (20)  /  demo  
File Role Description
Files folder image1.0.0 (4 directories)
  Plain text file 1.0.0.php Aux. Auxiliary script
  Plain text file index.php Example Example script

  Files folder image Files (20)  /  demo  /  1.0.0  
File Role Description
Files folder imagecontrollers (1 file)
Files folder imagecore (4 files)
Files folder imageviews (3 directories)
Files folder imagewidgets (1 file)

  Files folder image Files (20)  /  demo  /  1.0.0  /  controllers  
File Role Description
  Plain text file Welcome.php Class Class source

  Files folder image Files (20)  /  demo  /  1.0.0  /  core  
File Role Description
  Plain text file Backend_Controller.php Class Class source
  Plain text file Frontend_Controller.php Class Class source
  HTML file index.html Doc. Documentation
  Plain text file MY_Controller.php Class Class source

  Files folder image Files (20)  /  demo  /  1.0.0  /  views  
File Role Description
Files folder imagelayouts (1 file)
Files folder imagepartials (1 directory)
Files folder imagewidgets (1 file)

  Files folder image Files (20)  /  demo  /  1.0.0  /  views  /  layouts  
File Role Description
  Plain text file frondend.php Example Example script

  Files folder image Files (20)  /  demo  /  1.0.0  /  views  /  partials  
File Role Description
Files folder imagefrondend (3 directories)

  Files folder image Files (20)  /  demo  /  1.0.0  /  views  /  partials  /  frondend  
File Role Description
Files folder imagefooter (1 file)
Files folder imagehead (1 file)
Files folder imagepages (1 file)

  Files folder image Files (20)  /  demo  /  1.0.0  /  views  /  partials  /  frondend  /  footer  
File Role Description
  Plain text file footer.php Aux. Auxiliary script

  Files folder image Files (20)  /  demo  /  1.0.0  /  views  /  partials  /  frondend  /  head  
File Role Description
  Plain text file head.php Example Example script

  Files folder image Files (20)  /  demo  /  1.0.0  /  views  /  partials  /  frondend  /  pages  
File Role Description
  Plain text file hero.php Aux. Auxiliary script

  Files folder image Files (20)  /  demo  /  1.0.0  /  views  /  widgets  
File Role Description
  Plain text file frontend_nav.php Aux. Auxiliary script

  Files folder image Files (20)  /  demo  /  1.0.0  /  widgets  
File Role Description
  Plain text file Frontend_nav.php Class Class source

  Files folder image Files (20)  /  libraries  
File Role Description
  Plain text file Template.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:85
This week:0
All time:10,033
This week:673Up