Web Design and Web Development Forum
-
PEAR's HTML_Template_IT package.
Hi all
As the new contest has arrived i wanted to start sharing as its 5th Jan
As per requested, here is a basic tutorial on how to code with PEAR's HTML_Template_IT package.
Introduction
PEAR :: HTML_Template_IT is a common package used for web application templating. It allows flexible templating for your web development needs. The package allows your designer who knows nothing about all the complicated PHP coding to design a template that will work with your PHP code without ever need to touch any PHP books. HTLP_Template_IT uses place holders and data blocks which allows contents to be populated through PHP coding and generate the content of the page by design on the fly. Additionally, it can be used for both tabular design and CSS tableless design.
Getting Started
There are two parts to developing an HTML_Template_IT website; first part is the template itself, and the second is the coding. Personally, I think the template part is easier, so that's where I will begin. If you are already familar with the templating portion, please feel free to scroll down to the coding part and continue from there.
Creating a Template
To create a template using the HTML_Template_IT library is really easy. In fact, you do not even need to know HTML_Template_IT library. You will only need to know two things: 1) How to define a place holder, and 2) How to define a block scope.
Place Holders
Place holders are where you will be putting data from your backend engine. Place holders must be named only with alphanumeric characters. You must not include space, punctuations, or special characters such as acute accented e, or general western european/asian characters. This means you can only use A to Z, a to z, and 1 to 0 for your place holder naming. For example, if you want to create an contact list page, you will want to create place holders for "FirstName", "LastName", and "PhoneNumber". Place holders must also be surrounded by two curly bracelets, so from our above example, your place holders are "{FirstName}", "{LastName}", and "{PhoneNumber}".
Blocks
Blocks are used when something is to be repeated. For example, if you want to list the above data in a tabular structure, your blocks will be the cells. Like place holders, blocks must be named only with alphanumeric characters. You must not include space, punctuations, or special characters such as acute accented e, or general western european / asian characters. This means you can only use A to Z, a to z, and 1 to 0 for your block naming. Blocks can be initiated like comments in standard HTML 4.0 and preceeded with the special key word "BEGIN" and "END" to define the scope. As a general practice, the keywords should always be written in all upper case as above.
Creating a Template File
When you combine the two together, you will have yourself a small compact template file. If we assume that your template does nothing but listing the data from the database, this below can be a possible version of your template file.
template.list.html
CODE
<html>
<body>
<table>
<!-- BEGIN row -->
<tr>
<td>
{FirstName}
</td>
<td>
{LastName}
</td>
<td>
{PhoneNumber}
</td>
</tr>
<!-- END row -->
</table>
</body>
</html>
Coding Your Back Engine
In order to have your contents to populate your template, you must have a back end PHP script using the HTML_Template_IT library to process raw data and parse the template file we have created above. I will continue to use the above example through the rest of this tutorial so by the end of this tutorial, you will have a working page.
To cerate your PHP file, you will need a few things: 1) Data to populate the template with, 2) Instance of your HTML_Template_IT object, 3) Main loop to iterate and enter the contents and finally 4) Outputting the parsed template with all the data you have to the visitor. With that in mind, let's get started with the php backend.
Data for Template
Data for this process can be fed from a MySQL query, or just hardcoded data. For simplicity reasons, we will use a set of hardcoded data to prevent any confusion with MySQL information. I will also assume that you are already familiar with nested arrays so there will be no major explainations from me on this section. If you need further explaination as to how to use nested array, please feel free to private message me and I'll try my best to explain it to you. The data is very straight forward as follows:
list.php -- Data
CODE
<? php
$data = array (
"0" => array (
"FirstName" => "John",
"LastName" => "Doe",
"PhoneNumber" => "555-4023" ),
"1" => array (
"FirstName" => "Ann",
"LastName" => "Smith",
"PhoneNumber" => "555-1039" ),
"2" => array (
"FirstName" => "Andy",
"LastName" => "Hasten",
"PhoneNumber" => "555-5429" )
);
?>
Creating the HTML_Template_IT Instance
After you have downloaded the HTML_Template_IT package, you can place it anywhere you want on your server. Although there is no installation, you must make sure it is accessable by your script as you need to use it to include it as well as create instances of it to generate contents. Creating an instance of it is really easy, and its rather self explainatory; you require the class, create an instance of it, and get it to load the template you have previously created. It can be done as so:
list.php -- HTML_Template_IT Instance
CODE
<? php
require_once ("./includes/PEAR/HTML/Template/IT.php"); // This is where you placed the class library in relation to your script.
// Create a new instance of it and tell it where your template is located.
$tpl = new HTML_Template_IT("./templates");
// Tell it to load the template file, remove unknown variables and remove empty blocks
$tpl->loadTemplatefile("main.tpl.htm", true, true);
?>
Creating Your Main Loop
Because you have a data array, you will need to create a loop to loop through it in order to populate your template with the contents. This is typically done with a general "foreach" loop. Inside the loop, you want to tell the script to assign data to your place holders, as well as loop with your blocks so you have a presentable page when you are done. Without further delay, let's look at the code of the main loop for our address book listing script.
list.php -- Main Loop
CODE
<? php
foreach($data as $person) {
// Assign data to the inner block
$tpl->setCurrentBlock("row");
$tpl->setVariable("FirstName", $person["FirstName"]);
$tpl->setVariable("LastName", $person["LastName"]);
$tpl->setVariable("PhoneNumber", $person["PhoneNumber"]);
$tpl->parseCurrentBlock("row");
}
?>
Outputting Data for Your Visitor
Now that you've gotten HTML_Template_IT to read your template, parse content, and set the variables, you've pretty much done everything. All there is left to do is to present the data to the visitor or yourself, depending on who is visiting the page. This can be done really easily through the PEAR HTML_Template_IT module. In fact, it is only an one-liner!
list.php -- Output Data
CODE
<? php
$tpl->show();
?>
There you have it. Your code is all done and ready to be used!
I hope this reading have been interesting and useful for you. If you find any errors in the above, please post back to let me know and I will try to update it as soon as I can.
Cheers.
Reply With Quote -
Re: PEAR's HTML_Template_IT package.
Wow...great tutorial...thanx for the info...
Reply With Quote