Get Smarty


Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Buy cheap glasses from and save up to 80%.

Where to buy prescription glasses? is your best choice!

Visit for cheap soccer cleats, sneakers and Soccer Jerseys updates


We'll start with index.php, the entry point of our application. This is the file directly accessed by the web browser.



 * Project: Guestbook Sample Smarty Application
 * Author: Monte Ohrt <monte [AT] ohrt [DOT] com>
 * File: index.php
 * Version: 1.1

// define our application directory
define('GUESTBOOK_DIR', '/web/');
// define smarty lib directory
define('SMARTY_DIR', '/usr/local/lib/php/Smarty/');
// include the setup script
include(GUESTBOOK_DIR . 'libs/guestbook_setup.php');

// create guestbook object
$guestbook = new Guestbook;

// set the current action
$_action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'view';

switch($_action) {
    case 'add':
        // adding a guestbook entry
    case 'submit':
        // submitting a guestbook entry
        if($guestbook->isValidForm($_POST)) {
        } else {
    case 'view':
        // viewing the guestbook


The index.php file acts as the application controller. It handles all incoming browser requests and directs what actions to take. It will define our application directories, include the setup script, and direct an action depending on the action value from the $_REQUEST super-global. We will have three basic actions: add when a user wants to add an entry to the guestbook, submit when a user submits an entry, and view when the user displays the guestbook. The default action is view.



 * Project: Guestbook Sample Smarty Application
 * Author: Monte Ohrt 
 * File: guestbook_setup.php
 * Version: 1.1

require(GUESTBOOK_DIR . 'libs/guestbook.lib.php');
require(SMARTY_DIR . 'Smarty.class.php');

// smarty configuration
class Guestbook_Smarty extends Smarty {
    function __construct() {
      $this->setTemplateDir(GUESTBOOK_DIR . 'templates');
      $this->setCompileDir(GUESTBOOK_DIR . 'templates_c');
      $this->setConfigDir(GUESTBOOK_DIR . 'configs');
      $this->setCacheDir(GUESTBOOK_DIR . 'cache');

guestbook_setup.php is where we do some basic application configuration, such as our database and template configs. We will be using the PDO database abstraction library available from PHP 5. We will be using MySQL as our database, enter the appropriate dsn information for your database setup.

We will be needing a basic database setup. The following is a script that you can dump directly into MySQL with mysql < guestbook.sql. Be sure you change the GRANT line with your database/user information.




  id int(11) NOT NULL auto_increment,
  Name varchar(255) NOT NULL default '',
  EntryDate datetime NOT NULL default '0000-00-00 00:00:00',
  Comment text NOT NULL,
  PRIMARY KEY  (id),
  KEY EntryDate (EntryDate)

GRANT ALL ON GUESTBOOK.* to guestbook@localhost identified by 'foobar';

[Page 1] [Page 2] [Page 3] [Page 4]