Abstracting PHP from HTML with PEAR Templates

I decided to write this post because I was recently trying to explain this concept to a co-worker and was unable to find a lot of resources for this topic available so hopefully it ends up being useful to someone.

When I first started working at my company a few years ago and was introduced to the concept of PEAR templates, I absolutely hated them. However, I realized that the hatred stemmed from ignorance more than anything. The concept is actually pretty solid and allows for dynamic development of standardized software that can easily be ported to many applications. So, before you say that it isn't for you, read through this tutorial and if you still aren't convinced, well... that's ok too!

What Knowledge is Required for this Tutorial?
  • This tutorial assumes that you have access to the server where your website is being hosted or can contact the administrator to make sure PEAR templates are properly installed on the server.
  • This tutorial also assumes that you are familiar with basic PHP and HTML concepts.

Where are PEAR Templates Useful?
  • Websites with information that is constantly changing.
  • Websites where the design (visuals) is frequently changing.
  • Websites that are pulling information from a database (dynamic).

Installing/Setting Up PEAR Templates

In some cases, PEAR may have been installed when you installed PHP. It used to be packaged with some versions of PHP, but I am not sure if this is still the case. If it is already installed and configured, you can run the quick install of the template through the command prompt with the following:
pear install HTML_Template_IT-1.3.0

I ran the manual install from the PEAR HTML Template Download Page.

In the root folder of my website on the server, I created the following file structure:
  • HTML
    • Template

Within the Template folder, I copied the following files from the PEAR HTML Template Download:
IT.php
IT_Error.php
ITX.php


Let's see if it worked
  1. Create 2 Pages on your web server:
    test.php - This is the PHP page where you will perform all of the scripting and data pulls.



    test.tpl.htm - this is the html page where you will design the visual layout of the page.
  2. If it worked, this is what your page should look like in the web browser:


    Well... I'll be the first to admit, I'm pretty disappointed.

    This was really just to test to make sure all of your references were correct with the PEAR library. If the word TEST did not appear in the web browser a couple things could have happened:
    • Your require once tag in the php is signaling where to look for the library. Make sure the file is actually in that location on the server!
    • The $tpl->show(); command actually tells the template to render the visuals. Without this simple line of code, nothing will show up.
    • In the .tpl.htm page, {BLOCK_CONTENT} can be thought of as a variable. If at least one variable is not set within the block nothing with show up for that block. (There is a way around this that I will talk about later).
    • Still can't get it to work? Leave me a comment.
I'm going to continue this tutorial in a few installments. If you get stuck along the way, feel free to leave a comment and I will guide you through it.

Looking ahead

The next tutorial will go through the different elements of the templates in pear: Ex. Blocks, variables, how to use a touch block (which is calling a block when you do not have a variable to set), an alternative to touch blocks which I use sometimes (because I hadn't discovered the touch block documentation right away and developed my own method of accomplishing the same task) At the end of the next tutorial, I will select a simple website template from FourSquare and show you how to replicate it using PEAR templates.

Comments