smarty template engine
Monday, October 13, 2008  
download | documentation | faq | forum | mailing lists | changelog | contribs 


search for in the  


Sample Application: Guestbook

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>
 * Date: March 14th, 2005
 * File: index.php
 * Version: 1.0
 */

// 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 <monte [AT] ohrt [DOT] com>
 * Date: March 14th, 2005
 * File: guestbook_setup.php
 * Version: 1.0
 */

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

// database configuration
class GuestBook_SQL extends SQL {
    function 
GuestBook_SQL() {
        
// dbtype://user:pass@host/dbname
        
$dsn "mysql://guestbook:foobar@localhost/GUESTBOOK";
        
$this->connect($dsn) || die('could not connect to database');
    }       
}

// smarty configuration
class Guestbook_Smarty extends Smarty 
    function 
Guestbook_Smarty() {
        
$this->template_dir GUESTBOOK_DIR 'templates';
        
$this->compile_dir GUESTBOOK_DIR 'templates_c';
        
$this->config_dir GUESTBOOK_DIR 'configs';
        
$this->cache_dir 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 PEAR::DB library available from PEAR. Be sure DB.php is in your include_path, or supply an absolute path to it. We will be using MySQL as our database, enter the appropriate dsn information for your database setup.

NOTE: If you get a runtime error similar to Call to undefined function: query(), it is likely that your $dsn information is incorrect. Check it twice, test your db connection.

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] [Page 5]


 

credits 

Smarty Copyright © 2002-2008
New Digital Group, Inc.

All rights reserved.