How To: Create your own PHP Template Engine

Write your "How To" regarding anything you are comfortable with and you feel it will help the forum members.

NOTE :: All threads started here will appear only after the approval from Administrator
Post Reply
leo
Posts: 13
Joined: Tue Mar 13, 2007 4:56 am

How To: Create your own PHP Template Engine

Post by leo »

This howto will show you how to create a basic template engine in PHP.
Template engines are used in nearly all major web applications. It converts static HTML templates, into dynamic web pages. One of the most benefitial aspect of using template engines is that you can change the look of your website all by replacing a single template file!

So... let's get on to the code:
[PHP]
Class TemplateEngine {

var $page;

function GetTemplate($template = "template.tpl") {
if (file_exists($template))
$this->page = join("", file($template));
else
die ("The template file could not be located.");
}

function ReplaceTags($array = array()) {
if (sizeof($array) > 0)
foreach ($array as $tag => $data)
$this->page = ereg_replace("{".$tag."}", $data, $this->page);
}

function Output() {
echo ($this->page);
}

}[/PHP]

Although the code may be a little confusing, it is actually quite simple. (You need to have a basic understanding of classes and functions in PHP... I might add some more howtos later...)

We first start off by creating a new class called "TemplateEngine" with three functions.

1) GetTemplate( $template ) :
[INDENT]The GetTemplate function accepts only one argument, which is the name of the template you want to use. It opens the template file through file($template), and joins each line of the file using the join().
Of course, you can use fread() and fopen() to open your templates. The choice is up to you.
In fact, in my more advanced projects, I tend to store my templates in a database, and I simply query for the appropriate template from the database in the GetTemplate function.[/INDENT]

2) ReplaceTags ( $array ):
[INDENT]The ReplaceTags function accepts an array as its only argument. This array specifies the tags and values that need replacing. I will explain this later in this howto.[/INDENT]

3) Output ()
[INDENT]The Output function simply echos out the finished page.[/INDENT]

---------------------------

So, now you have the basic template engine, how do you use it?
Here is an example:

mypage.tpl:

Code: Select all

<html>
<head>
<title>{title}</title>
</head>
<body>
<h3>Welcome to {sitename}!</h3>
This website is hosted on {siteurl}. 

Your name is {yourname}
</body>
</html>
index.php:
[PHP]<?php
include "template.inc.php"; // the template engine (the code above)

$Template = new TemplateEngine;
$Template -> GetTemplate('mypage.tpl');
$Template -> ReplaceTags(array('title' => 'Website Title',
'sitename' => 'My Website',
'siteurl' => $_SERVER['SERVER_NAME'],
'yourname' => 'Bob Smith'));
$Template -> Output();

?>[/PHP]


If everything works out, the result would be something like:

Code: Select all

Welcome to My Website!
This website is hosted on server.steamr.com. 
Your name is Bob Smith
With the title set as "Website Title"


------------------------------------------

Although the above example is plain, template engines can make your projects easier to manage and code. Keep in mind that you can use the template engine to replace tags for color schemes, stylesheets, usernames, dates, entire articles, anything you want. You can even use the template engine to draw entire sites without having to code PHP in your HTML code again!

If you need additional help, don't hesitate to ask :)
Attachments
template.zip
(916 Bytes) Downloaded 89 times


SHAdmin
Posts: 2089
Joined: Sat Dec 18, 2004 11:28 am
Contact:

Post by SHAdmin »

You have been credited 40 points for sharing such a usefull "How To" with the community members.
Post Reply