Introducing Smarty: A PHP Template Engine

Most PHP developers go through scores of changes in regards to their development
expertise. They usually start by creating simple scripts to add dynamic features
to their Web sites, then go on to add new features. This process leads to more complexity with the mix of PHP and HTML. It is quite common to see PHP
scripts that include other files, with the ultimate objective of reusing code and HTML widgets. One that includes file outputs--the standard header of the site; another one the standard table, and so on.

The Reasoning Behind Templates

While that process is certainly valid and useful, most developers agree that the separation of business logic and layout logic makes the code a lot easier to
understand and maintain. This is the reason behind templates, to separate
business logic from layout.

What Smarty Has to Offer

Smarty is a somewhat new development in the PHP world, and it brings several
new and unique features. One of these unique features is that
Smarty 'compiles' the parsed templates into PHP scripts, and then reuses the
compiled template when appropriate. Obviously, this brings a huge
performance improvement over other template solutions, as the main PHP script doesn't need to parse and output the same template on every request.

Smarty also has support for plug-in modules, allowing developers to
create their own set of special functions and have Smarty recognize them. And it has built-in caching support, special constructs that can be used on templates to control the format of the layout, and much more.

Most importantly, Smarty gives developers tools that let them separate the business-logic code from the layout-formatting code. And Smarty goes one step further by allowing developers to put control-flow structures in the template source. This might sound a bad idea,
since it would imply business-like logic in the template, but it is actually quite useful--you can tell Smarty to use a specific color on table X on the template itself, instead of having PHP code do this work. After all, this is template-related information.

One quick template example might be of value after this quick introduction:

Installing Smarty

Smarty is quite simple to install, and I'll give step-by-step instructions on how to do just that. First, you must download the distribution.

After downloading and extracting the files from the tarball, copy the resulting
Smarty directory to some place inside your include_path. A good option is to copy this directory in the PEAR library directory (see my previous articles about PEAR). In Unix environments, it will usually be:

$ cp -R Smarty /usr/local/lib/php/

In Windows computers, you will need to copy the Smarty directory to
'C:\php\pear'.

Organizing Your Application to Use Smarty

There are several ways to organize your application to make use of Smarty, but
I'll document here what I always do to structure my Web sites and applications.

Make sure to give the Apache user write access to the templates_c subdirectory.
This is the directory that Smarty uses to store the compiled templates. You can
do this by using the following commands in a Unix system: