Now: Tutorial for Web and Software Design > PHP > PHP Basic > PHP Content
> Introducing Smarty: A PHP Template Engine [Bookmark it]
Introducing Smarty: A PHP Template Engine

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.

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.

[1] [2] [3] Next

[Bookmark][Print] [Close][To Top]
  • Prev Article-PHP:

  • Next Article-PHP:
  • Related Materias
    Scaling Dynamic Websites w
    Whats New in ModSecurity
    A Day in the Life of #Apac
    Important Notice for Apach
    Custom-Compiling Apache an
    Apaches eXtended Server Si
    A Day in the Life of #Apac
    A Day in the Life of #Apac
    A Day in the Life of #Apac
    A Day in the Life of #Apac
    Topics
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Graphic Design Tutorial
     

    Coreldraw Tutorial

      Illustrator Tutorial
      3D Graphics Articles
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial&Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial&Articles
     

    XML Style Tutorial

      AJAX Tutorial
      XML Mobile
    Flash Tutorial&Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial&Articles
     

    Linux Tutorial

      Symbian Tutorial
      MacOS Tutorial