Publishing System Settings Logout Login Register
Pear Module: HTML_Template_Sigma
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on July 23rd, 2005
6977 views
PHP Coding
HTML_Template_Sigma
This tutorial is about Pear's HTML_Template_Sigma and how to use it.

Table of Content:
1. Introduction to PEAR
2. Introduction to HTML_Template_Sigma
3. Installing HTML_Template_Sigma
4. Using HTML_Template_Sigma

1. Introduction to PEAR
What is PEAR? (from the PEAR website)


PEAR is short for "PHP Extension and Application Repository" and is pronounced just like the fruit. The purpose of PEAR is to provide:

- A structured library of open-sourced code for PHP users
- A system for code distribution and package maintenance
- A standard style for code written in PHP
- The PHP Foundation Classes (PFC)
- The PHP Extension Community Library (PECL)
- A web site, mailing lists and download mirrors to support the PHP/PEAR community

PEAR is a community-driven project with the PEAR Group as the governing body. The project has been founded by Stig S. Bakken in 1999 and quite a lot of people have joined the project since then.


How do I install PEAR?
The installation guide on how to install PEAR

2. Introduction to HTML_Template_Sigma
HTML_Template_Sigma is a PEAR package, a fully featured templating engine.
Templating engines have the purpose of seperating the HTML from the PHP in your PHP files. This keeps the code clean and readable.
This also allows you to easily edit the HTML without having to browse through all your PHP files looking for the HTML code.
HTML_Template_Sigma has many features, like replacement variables, loops and many others.

3. Installing HTML_Template_Sigma
HTML_Template_Sigma can be installed in many different ways. The easiest way is the PEAR package manager. (Root access is required for this)
(requires the latest version of the PEAR package manager to be installed.)

Automatic Installation:
Go to your shell and run:
$ pear install <package>

Replace with the name of the package you wish to install.
PEAR package manager now downloads and installs the specified package.

Semi-Automatic Installation:
Another way to install a package offline is putting the package in your PEAR dir.
Then run the following command:
$ pear install <file>.tgz

This installs the package (downloaded as a .tgz) without needing to have an internet connection.

Manual Installation:
To install the package manually, let's put the package in /includes/
Use the following code to tell PHP where to look for our package:
<?php
ini_set(\"include_path\", '/var/www/www.example.com/includes/' . PATH_SEPARATOR . ini_get(\"include_path\"));
?>


Now we have set our include_path, we can simply require our package like this:
<?php
require_once 'HTML/Template/Sigma.php';
?>


After installing HTML_Template_Sigma, read on for the instructions on how to use it.

4. Using HTML_Template_Sigma
This step of the tutorial will teach you the very basics of using HTML_Template_Sigma.
We will be learning:

- Assigning replacement variables
- Working with loops
- Printing or returning the template?

The template files
Template files are usually put in ./templates/ or ./skin/, but that does not really matter.
Below this line you will find an example template file:
<html>
<table border>
<!-- BEGIN row -->
<tr>
<!-- BEGIN cell -->
<td>
{DATA}
</td>
<!-- END cell -->
</tr>
<!-- END row -->
</table>
</html>


states the beginning of a loop. Everytime this loop is parsed, it will parse this.
You have to end every loop with (replace with the name of your loop)

{DATA} is a replacement variable. When assigning a variable in your PHP file, you can use it like this.

Let's look at our PHP file now: (HTML_Template_Sigma's example that came with the package)
<?php
require_once \"HTML/Template/Sigma.php\";

$data = array (
\"0\" => array(\"Stig\", \"Bakken\"),
\"1\" => array(\"Martin\", \"Jansen\"),
\"2\" => array(\"Alexander\", \"Merz\")
);

$tpl = new HTML_Template_Sigma(\"./templates\");

$tpl->loadTemplatefile(\"main.tpl.htm\", true, true);

foreach($data as $name) {
foreach($name as $cell) {
// Assign data to the inner block
$tpl->setCurrentBlock(\"cell\");
$tpl->setVariable(\"DATA\", $cell);
$tpl->parseCurrentBlock(\"cell\");
}
// Assign data and the inner block to the
// outer block
$tpl->setCurrentBlock(\"row\");
$tpl->parseCurrentBlock(\"row\");
}
// print the output
$tpl->show();
?>


I will now explain the code above bit by bit, so you will have an idea why and how it is used.

require_once \"HTML/Template/Sigma.php\";

Without this, the whole thing will not work. We need to require the PEAR package.

$data = array (
\"0\" => array(\"Stig\", \"Bakken\"),
\"1\" => array(\"Martin\", \"Jansen\"),
\"2\" => array(\"Alexander\", \"Merz\")
);

Assign an array of data. We are looping through this later on.

$tpl = new HTML_Template_Sigma(\"./templates\");

This initiates the HTML_Template_Sigma class and sets the path to the templates.

$tpl->loadTemplatefile(\"main.tpl.htm\", true, true);

Load a template file called main.tpl.htm.

foreach($data as $name) {
foreach($name as $cell) {
// Assign data to the inner block
$tpl->setCurrentBlock(\"cell\");
$tpl->setVariable(\"DATA\", $cell);
$tpl->parseCurrentBlock(\"cell\");
}
// Assign data and the inner block to the
// outer block
$tpl->setCurrentBlock(\"row\");
$tpl->parseCurrentBlock(\"row\");
}

We are looping through the data, assign the variables using setVariable and parse loop blocks using setCurrentBlock() and parseCurrentBlock().

// print the output
$tpl->show();

This outputs the template to the screen. If you want to return the template instead, use:
$template = $tpl->set();


I hope you learnt a great deal of PEAR and HTML_Template Sigma by reading this tutorial.
Be sure to use HTML_Template_Sigma in your own projects!
Premium Publisher
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
Ruben K

i am totally awesome
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top