signalkraft

Inherit Template Parser

…for the Codeigniter PHP Framework

This template parser extends the existing Codeigniter Parser with inheritance and blocks, inspired by the excellent Django Python Framework.

Important Features

DRY — Don’t repeat yourself

This simple parser aims to remove 99% of all code redundancy in your Codeigniter templates, without the complications of a much larger templating engine like Smarty. Inherit extends the Codeigniter Parser, is designer-friendly and relies entirely on a proven concept.

Repeating yourself is bad, even and especially in your templates. In PHP we are used to inheriting Classes to replicate certain functionality and have consistent functions and variables we can fall back on — it’s what makes Codeigniter so great. But the same should be true for your templates, shouldn’t it? Instead you write a lot of include("this") and include("that").

Maybe slightly worse, you might load header and footer in your controller and put them in variables for the view to use — making it difficult for the designer to trace back where that header content is coming from and how he can change it. Stick around and read the example to find out about a better solution.

Installation

Download the library, extract the contents to your system folder. Put it in your autoload config or load it in your controller. See the example below for a quick howto.

Example

Instead of needlessly splitting your website into a header, a footer, and everything in between we will now create a base template:

application/views/base.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
	<title>{block="title"}My Website!{/block}</title>

	<link rel="stylesheet" href="css/style.css" type="text/css" />
</head>

<body>
<h1>Header!</h1>
<div id="content">
{block="content"}Default content, nobody probably reads this.{/block}
</div>
<p>{block="footer"}Footer, copyright me 2010.{/block}</p>
</body>
</html>

This is our base, all the different controllers simply extend it:

application/views/welcome.php
{extends="base"}

{block_prepend="title"}Home - {/title}
{block="content"}Hello World, this is my welcome page.{/content}

Now you simply call $this->parser->parse("welcome"); and the output looks like this:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
	<title>Home - My Website!</title>

	<link rel="stylesheet" href="css/style.css" type="text/css" />
</head>

<body>
<h1>Header!</h1>
<div id="content">
Hello World, this is my welcome page.
</div>
<p>Footer, copyright me 2010.</p>
</body>
</html>

Notice how it prepended the HTML title, left the footer alone and completely overwrote the content? It’s all thanks to inheritance, and it works just like it does in PHP5.

For a more “real-world” example, you could extend base.php with templates called one_column and two_column, then extend those with your pages. Now, if you ever wanted to add a second column to your welcome screen, just point the {extends} block to two_column, instead of one_column. It couldn’t be easier, and there is zero code overhead.

Performance Caveat

This is the first public release of Inherit. There’s gonna be bugs and there definitely are huge performance improvements waiting to happen. Neither stopped me from successfully using this library on several production websites. If you’re experiencing a high volume of visitors, wait until I put in Caching in the next major release. For your personal blog or portfolio it will probably hold up, performance-wise.

More Functions

Inherit includes a language file simply called “views”. In it you will find a multi-dimensional array of this format:

application/language/english/views_lang.php
<?php

    $lang['welcome'] = array(
		'title' => 'My welcome title'
	);
	
	$lang['blog/index'] = array(
		'title' => 'My blog title',
		'archive' => 'View the blog archives'
	);

?>

The array associated with each view URI is available within that view as $lang. In this example, calling $lang['title'] on your welcome page will return a different value than calling it on your blog index.

Themes

Basically, calling $this->parser->theme('blue'); $this->parser->parse('welcome'); will attempt to parse the file views/blue/welcome.php. If it doesn’t exist, the default view is used instead (views/welcome.php). So even your themes inherit a parent, if you want them to.

Similarly, if views/blue/welcome.php extends a template, it will first look for that parent in views/blue/, then in the default theme folder (usually views/).

You can tell Inherit to look for the default views somewhere else, if you set the appropriate variable in MY_Parser.php. Watch this section for more comprehensive documentation in the future.