Get Smarty

Donate

Paypal

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

Advertisement

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

/web/www.example.com/docs/guestbook/index.php

<?php

/**
 * 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/www.example.com/smarty/guestbook/');
// 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
        $guestbook->displayForm();
        break;
    case 'submit':
        // submitting a guestbook entry
        $guestbook->mungeFormData($_POST);
        if($guestbook->isValidForm($_POST)) {
            $guestbook->addEntry($_POST);
            $guestbook->displayBook($guestbook->getEntries());
        } else {
            $guestbook->displayForm($_POST);
        }
        break;
    case 'view':
    default:
        // viewing the guestbook
        $guestbook->displayBook($guestbook->getEntries());        
        break;   
}

?>

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.

/web/www.example.com/smarty/guestbook/libs/guestbook_setup.php

<?php

/**
 * 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() {
      parent::__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.

guestbook.sql

CREATE DATABASE GUESTBOOK;

USE GUESTBOOK;

CREATE TABLE GUESTBOOK (
  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)
) TYPE=MyISAM;

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

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

Advertisement

Sponsors [info]

Sponsors