What is Smarty?
Why use it?
Use Cases and Work Flow
Syntax Comparison
Template Inheritance
Best Practices
Crash Course
You may use the Smarty logo according to the trademark notice.
For sponsorship, advertising, news or other inquiries, contact us at:
<?php
/**
* Project: Guestbook Sample Smarty Application
* Author: Monte Ohrt <monte [AT] ohrt [DOT] com>
* File: guestbook.lib.php
* Version: 1.1
*/
/**
* guestbook application library
*
*/
class Guestbook {
// database object
var $pdo = null;
// smarty template object
var $tpl = null;
// error messages
var $error = null;
/* set database settings here! */
// PDO database type
var $dbtype = 'mysql';
// PDO database name
var $dbname = 'GUESTBOOK';
// PDO database host
var $dbhost = 'localhost';
// PDO database username
var $dbuser = 'guestbook';
// PDO database password
var $dbpass = 'foobar';
/**
* class constructor
*/
function __construct() {
// instantiate the pdo object
try {
$dsn = "{$this->dbtype}:host={$this->dbhost};dbname={$this->dbname}";
$this->pdo = new PDO($dsn,$this->dbuser,$this->dbpass);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage();
die();
}
// instantiate the template object
$this->tpl = new Guestbook_Smarty;
}
/**
* display the guestbook entry form
*
* @param array $formvars the form variables
*/
function displayForm($formvars = array()) {
// assign the form vars
$this->tpl->assign('post',$formvars);
// assign error message
$this->tpl->assign('error', $this->error);
$this->tpl->display('guestbook_form.tpl');
}
/**
* fix up form data if necessary
*
* @param array $formvars the form variables
*/
function mungeFormData(&$formvars) {
// trim off excess whitespace
$formvars['Name'] = trim($formvars['Name']);
$formvars['Comment'] = trim($formvars['Comment']);
}
/**
* test if form information is valid
*
* @param array $formvars the form variables
*/
function isValidForm($formvars) {
// reset error message
$this->error = null;
// test if "Name" is empty
if(strlen($formvars['Name']) == 0) {
$this->error = 'name_empty';
return false;
}
// test if "Comment" is empty
if(strlen($formvars['Comment']) == 0) {
$this->error = 'comment_empty';
return false;
}
// form passed validation
return true;
}
/**
* add a new guestbook entry
*
* @param array $formvars the form variables
*/
function addEntry($formvars) {
try {
$rh = $this->pdo->prepare("insert into GUESTBOOK values(0,?,NOW(),?)");
$rh->execute(array($formvars['Name'],$formvars['Comment']));
} catch (PDOException $e) {
print "Error!: " . $e->getMessage();
return false;
}
return true;
}
/**
* get the guestbook entries
*/
function getEntries() {
try {
foreach($this->pdo->query(
"select * from GUESTBOOK order by EntryDate DESC") as $row)
$rows[] = $row;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage();
return false;
}
return $rows;
}
/**
* display the guestbook
*
* @param array $data the guestbook data
*/
function displayBook($data = array()) {
$this->tpl->assign('data', $data);
$this->tpl->display('guestbook.tpl');
}
}
?>
guestbook.lib.php is our application class. It contains the main logic for our entire application. Lets look at each class method.
/**
* class constructor
*/
function __construct() {
// instantiate the pdo object
try {
$dsn = "{$this->dbtype}:host={$this->dbhost};dbname={$this->dbname}";
$this->pdo = new PDO($dsn,$this->dbuser,$this->dbpass);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage();
die();
}
// instantiate the template object
$this->tpl = new Guestbook_Smarty;
}
This is the class constructor. It is executed each time we instantiate the guestbook object. It instantiates the PDO and Smarty objects as properties. We can then access them from within our object methods.
/**
* display the guestbook entry form
*
* @param array $formvars the form variables
*/
function displayForm($formvars = array()) {
// assign the form vars
$this->tpl->assign('post',$formvars);
// assign error message
$this->tpl->assign('error', $this->error);
$this->tpl->display('guestbook_form.tpl');
}
The displayForm() method is used for displaying the guestbook entry form. It assigns the form variables and the form validation error message to the template, then displays the form.
/**
* fix up form data if necessary
*
* @param array $formvars the form variables
*/
function mungeFormData(&$formvars) {
// trim off excess whitespace
$formvars['Name'] = trim($formvars['Name']);
$formvars['Comment'] = trim($formvars['Comment']);
}
The mungeFormData() method trims off whitespace from the form input. This is called prior to form validation. Notice the form data is passed into the method by reference so the changes will affect the original array.
/**
* test if form information is valid
*
* @param array $formvars the form variables
*/
function isValidForm($formvars) {
// reset error message
$this->error = null;
// test if "Name" is empty
if(strlen($formvars['Name']) == 0) {
$this->error = 'name_empty';
return false;
}
// test if "Comment" is empty
if(strlen($formvars['Comment']) == 0) {
$this->error = 'comment_empty';
return false;
}
// form passed validation
return true;
}
The method isValidForm() validates the form input. This is a simple test to see if the Name or Comment was empty. If so, the appropriate error code is assigned to the error property. (These error codes are used by the template later on.)
/**
* add a new guestbook entry
*
* @param array $formvars the form variables
*/
function addEntry($formvars) {
try {
$rh = $this->pdo->prepare("insert into GUESTBOOK values(0,?,NOW(),?)");
$rh->execute(array($formvars['Name'],$formvars['Comment']));
} catch (PDOException $e) {
print "Error!: " . $e->getMessage();
return false;
}
return true;
}
The addEntry method enters a new guestbook entry into the database. Note that PDO takes care of SQL escapement for your form vars.
/**
* get the guestbook entries
*/
function getEntries() {
try {
foreach($this->pdo->query(
"select * from GUESTBOOK order by EntryDate DESC") as $row)
$rows[] = $row;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage();
return false;
}
return $rows;
}
The method getEntries() gets all the guestbook entries from the database.
/**
* display the guestbook
*
* @param array $data the guestbook data
*/
function displayBook($data = array()) {
$this->tpl->assign('data', $data);
$this->tpl->display('guestbook.tpl');
}
The method displayBook() displays the guestbook entries. The $data array is expected to be an array of the guestbook entries. This is assigned to the template and then the template is displayed.