advertisement

Learn how to use the PEAR HTML_Template_IT Package to create a templating system for your website using PHP and HTML. After this tutorial you should be able to have a strong inital grasp on creating templates using PEAR and the HTML_Template_IT package. This is great for creating template files (.tpl files) and making your website more easily editable. It also helps organize your website when it gets big with lots of PHP files and you can change certain aspects of your site easily as well.

To get started your gonna need to download the HTML_Template_IT package. The link to this package is here :

http://pear.php.net/package/HTML_Template_IT/download

Click the download link, you will then be prompted to download a .tgz file. At the time of writing this tutorial the latest version of this package is 1.2.1.

Then I recommend you use WinRAR to un-compress the file you just downloaded. If you do not already have WinRAR you can get it here :

http://www.rarlabs.com/download.htm

Just go there and choose your language.

After un-compressing this file you will get a new folder, in this folder will be 1 file and another folder. Open the other folder, it is called ‘HTML_Template_IT-1.2.1’. Then you will see 2 more folders and 4 files. We are interested in just 2 of these files. The file called ‘IT.php’ and the file called ‘IT_Error.php’.

Alright now that we have our bearings we can get started, hang onto these 2 files and dont close that folder yet we will be using them in the next part of this tutorial.

Now, for this tutorial we will just be creating a simple 4 column table and we wont be using any dynamic data but I WILL show you how to do the same thing with dynamic data (Pulling data from a database and using a loop to display it).

So for this tutorial upload your IT.php and IT_Error.php files onto your website, upload them to the same. I am going to be uploading my files to the root directory on my website. Now create a new folder, call it ‘templates’. this is where you can store all your template files (.tpl files).

Now we are gonna create our first (and only) .tpl file. This will be our table template. Here is the entire code, I will give you all the code then explain it more in detail afterwards.

<html>
<body>
<table cellpadding="2" cellspacing="5">
<tr>
<th>Name</th>
<th>Birthday</th>
<th>Address</th>
<th>Fav Color</th>
</tr>
<!-- BEGIN TABLE -->
<tr>
<td>{FIRSTNAME}{LASTNAME}</td>
<td>{BIRTH}</td>
<td>{ADDRESS}</td>
<td>{COLOR}</td>
</tr>
<!-- END TABLE -->
</table>
</body>
</html>

Alright, now to explain the code. We first start off with defining out basic <html> and <body> tags. Then we create a table, I give it some padding and spacing so it doesn’t all look clumped together. Then I use <th> tags to create table headings. I create 4 table headings since I did say we will have 4 columns. So that is our first <tr> tag, just defining the table headers. Next we create something that is called a Block. A block is a block of information or code that we can present or duplicate. We use these blocks in our HTML by making HTML comments. I call this block TABLE.

Then we have our traditional <td> tags and everything, except instead of putting actual information in between the <td> tags, we use something called Placeholders. These place holders are used to define a certain part of information that we later give a value to in our PHP script.

So I create 5 place holders, for the name column I have 2 placeholders for a first and last name.

Now onto the PHP part of our tutorial, this is where we will parse our blocks in our template file and give our place holders values. Hopefully after this you will have a basic idea of how to use templates.

<?php
require_once "IT.php";

$template = new HTML_Template_IT("./templates");
$template->loadTemplatefile("table.tpl", true, true);

$template->setCurrentBlock("ADDRESS");
$template->setVariable("FIRSTNAME", "Sean");
$template->setVariable("LASTNAME", "McKnight");
$template->setVariable("BIRTH", "April 14th, 2006");
$template->setVariable("ADDRESS", "123 Fake Street");
$template->setVariable("COLOR", "<font color=\"red\">Red</font>");

$template->parseCurrentBlock();

$template->show();
?>

So we start off by defining our contact form and we will address your comments or questions!

Thanks!
Sean

advertisement