PHP DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our LAMP news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Listen Print Subscribe to PHP Subscribe to Newsletters

Introducing Smarty: A PHP Template Engine

by Joao Prado Maia
09/05/2002

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.

Web Database Applications with PHP, and MySQL

Related Reading

Web Database Applications with PHP, and MySQL
By Hugh E. Williams, David Lane

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:

{include file="header.tpl.html"}

Welcome to my Web site!

IP Address: {$smarty.server.REMOTE_ADDR}

{include file="footer.tpl.html"}

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.

My directory structure is as follows:

docs
  index.php
  configs (directory)
  templates (directory)
  templates_c (directory)

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:

$ mkdir templates_c
$ chown nobody:nobody templates_c
$ chmod 700 templates_c

Please note that I'm assuming that the Apache user is nobody, so you should change the line above accordingly.

Pages: 1, 2, 3

Next Pagearrow




Recommended for You

Tagged Articles

Post to del.icio.us

This article has been tagged:

php

Articles that share the tag php:

Understanding MVC in PHP (477 tags)

The PHP Scalability Myth (123 tags)

The Dynamic Duo of PEAR::DB and Smarty (53 tags)

PHP Form Handling (43 tags)

Very Dynamic Web Interfaces (39 tags)

View All

smarty

Articles that share the tag smarty:

The Dynamic Duo of PEAR::DB and Smarty (42 tags)

Introducing Smarty: A PHP Template Engine (6 tags)

Three-Tier Development with PHP 5 (5 tags)

View All

oreilly

Articles that share the tag oreilly:

What Is Web 2.0 (2327 tags)

A Simpler Ajax Path (19 tags)

Ajax on Rails (17 tags)

Wireless Mesh Networking (15 tags)

Understanding MVC in PHP (12 tags)

View All

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2010, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com