logo
Home :: Source Codes :: Tutorials :: Search :: Site Map :: Links :: Feedback   

Smarty

Smarty is a template engine written in PHP. Using Smarty, you can easily and cleanly separate your PHP back-end code from the front-end output, which makes your code much cleaner, more readable and thus managable.

Installation

Installing Smarty is pretty simple:
  • Download the Smarty package from http://smarty.php.net/download.php
  • Extract the file you downloaded. You should now get a directory named Smarty-x.y.z/ (x.y.z being the version).
  • Browse in the directory and find a directory named libs/. Copy the contents of that libs/ directory to a directory that's inside PHP's include_path. For example:
    mkdir /wwwroot/libs; cp -R Smarty-x.y.z/libs/* /wwwroot/libs/
    (where /wwwroot/ is your web Document Root).
  • Under your web Document Root, create 2 directories named templates/ and templates_c/
  • Your Document Root should now have 3 directories: libs/, templates/ and templates_c/
  • Make sure the web server (e.g.: Apache) has read and write permission to directory templates_c/.
  • Installation steps completed.

Understanding the File Structure

Your PHP files can be located anywhere under the Document Root, like how you normally do without Smarty.

The templates/ directory is where you create your Smarty files in. A Smarty file is basically just an HTML / XHTML file, with some Smarty variables in it. Normally we use .tpl extension for Smarty file names.

The templates_c/ directory is where Smarty put its "compiled" codes. You will never need to do anything to this directory (other than making sure it exists and readable and writable by the web server).

Valid XHTML 1.0!
Copyright © 2007 www.php-etc.com all rights reserved.