signalkraft

Inherit Template Parser

…for the Codeigniter PHP Framework

This tem­plate parser extends the exist­ing Codeigniter Parser with inher­i­tance and blocks, inspired by the excel­lent Django Python Frame­work.

Impor­tant Features

DRY — Don’t repeat yourself

This sim­ple parser aims to remove 99% of all code redun­dancy in your Codeigniter tem­plates, with­out the com­pli­ca­tions of a much larger tem­plat­ing engine like Smarty. Inherit extends the Codeigniter Parser, is designer-friendly and relies entirely on a proven concept.

Repeat­ing your­self is bad, even and espe­cially in your tem­plates. In PHP we are used to inher­it­ing Classes to repli­cate cer­tain func­tion­al­ity and have con­sis­tent func­tions and vari­ables we can fall back on — it’s what makes Codeigniter so great. But the same should be true for your tem­plates, 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 con­troller and put them in vari­ables for the view to use — mak­ing it dif­fi­cult for the designer to trace back where that header con­tent is com­ing from and how he can change it. Stick around and read the exam­ple to find out about a bet­ter solution.

Instal­la­tion

Down­load the library, extract the con­tents to your system folder. Put it in your autoload con­fig or load it in your con­troller. See the exam­ple below for a quick howto.

Exam­ple

Instead of need­lessly split­ting your web­site into a header, a footer, and every­thing in between we will now cre­ate 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 dif­fer­ent con­trollers sim­ply 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 sim­ply call $this->parser->parse("welcome"); and the out­put 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 com­pletely over­wrote the con­tent? It’s all thanks to inher­i­tance, and it works just like it does in PHP5.

For a more “real-world” exam­ple, you could extend base.php with tem­plates called one_column and two_column, then extend those with your pages. Now, if you ever wanted to add a sec­ond col­umn to your wel­come screen, just point the {extends} block to two_column, instead of one_column. It couldn’t be eas­ier, and there is zero code overhead.

Per­for­mance Caveat

This is the first pub­lic release of Inherit. There’s gonna be bugs and there def­i­nitely are huge per­for­mance improve­ments wait­ing to hap­pen. Nei­ther stopped me from suc­cess­fully using this library on sev­eral pro­duc­tion web­sites. If you’re expe­ri­enc­ing a high vol­ume of vis­i­tors, wait until I put in Caching in the next major release. For your per­sonal blog or port­fo­lio it will prob­a­bly hold up, performance-wise.

More Func­tions

Inherit includes a lan­guage file sim­ply 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 asso­ci­ated with each view URI is avail­able within that view as $lang. In this exam­ple, call­ing $lang['title'] on your wel­come page will return a dif­fer­ent value than call­ing it on your blog index.

Themes

Basi­cally, call­ing $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 par­ent, if you want them to.

Sim­i­larly, if views/blue/welcome.php extends a tem­plate, it will first look for that par­ent in views/blue/, then in the default theme folder (usu­ally views/).

You can tell Inherit to look for the default views some­where else, if you set the appro­pri­ate vari­able in MY_Parser.php. Watch this sec­tion for more com­pre­hen­sive doc­u­men­ta­tion in the future.