PHP As Template Engine

You need a template engine? I’m sure there are many available in Java and other languages, but then you need to learn their syntax before you can use them. PHP features make it possible for you to make a template engine in less than 10 lines of code. Want to know how? Note that this is not a new idea. CodeIgniter and Kohana framework have used it for their popular MVC model (and basically this article is inspired by it).

There are at least 3 great features of PHP which enable us to generate templates easily.

First is dynamic variable generation. Code like this:
$varname = 'whatever';
$$varname = 3;
echo $whatever;

will generate a variable named ‘whatever’ on the fly and assign it a value of 3.

Second is output buffering. With output buffering, you can store whatever should be outputted by a php file into a variable. Code like this:
ob_start();
include 'somefile.php';
$content = ob_get_clean();

will get anything echoed in ‘somefile.php’ and store it into variable $content.

The last is all-we-have-been-familiar-with include function. What’s special about PHP include is that it works as if the content of the file included ‘copy-pasted’ into the current script. So what? It means not only variables in included file will be carried onto the current script, but also variables we define in current script before ‘include’ will be carried onto the included file. For example, we can make two files like these:
file1.php
$a = 3;
include 'file2.php';

file2.php
$a = $a + 4;
echo $a;

and when we execute file1.php, we will get a 7.

Combining all 3 features, we will get a template engine in just one function:
function read_template($file, $param = array()){
ob_start();
foreach ($param as $k=>$v){
$$k = $v;
}
include $file;
$str = ob_get_clean();
echo $str;
}

Suppose we have template.php containing a very very simplistic html template

<html>
<head><title><?= $thetitle ?></title></head>
<body><?= $thebody ?></body>
</html>

Then we use the function this way:
$content = read_template('template.php', array('thetitle'=>'Hello there', 'thebody'=>'Pal'));
//echo or write $content into a file

That’s it. Really simple and powerful. If you want a more object-oriented approach, you can make a template class and store the variables as properties (I think that’s what CI and Kohana View class is doing). Hope this article is useful for you. Correct me if anything wrong.

Advertisements
This entry was posted in Programming and tagged , , , , . Bookmark the permalink.

3 Responses to PHP As Template Engine

  1. petra says:

    nice post, gan ^_^

  2. Captain_kurO says:

    They actually did this. I have already read KohanaPHP source code (some, not all yet) and found many extraordinary thing such as this.

  3. petra says:

    well, actually ada template engine yakni smarty, mereka cukup powerful karena menyediakan cache 😛

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s