什么是 Smarty?
为什么要使用它?
用例和工作流
语法比较
模板继承
最佳实践
速成课程
<?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 是我们的应用程序类。它包含我们整个应用程序的主要逻辑。让我们看看每个 class 方法。
/** * 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; }
这是 class 构造函数。每当我们实例化 guestbook 对象时,就会执行它。它将 PDO 和 Smarty 对象实例化为属性。然后我们就可以从 our object 方法中访问它们。
/** * 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'); }
displayForm() 方法用于显示留言簿条目表单。它将表单变量和表单验证错误消息分配给模板,然后显示表单。
/** * 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']); }
mungeFormData() 方法去除表单输入中的空白。在表单验证之前调用此方法。请注意,通过引用将表单数据传递给该方法,以便更改影响原始数组。
/** * 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; }
方法 isValidForm() 验证表单输入。这是一个简单的测试,目的是查看名称或评论是否为空。如果为空,则将相应的错误代码分配给 error 属性。(这些错误代码稍后由模板使用。)
/** * 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; }
addEntry 方法将新留言簿条目输入数据库。请注意,PDO 会自动对表单变量进行 SQL 转义。
/** * 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; }
getEntries() 方法从数据库中获取所有留言簿条目。
/** * display the guestbook * * @param array $data the guestbook data */ function displayBook($data = array()) { $this->tpl->assign('data', $data); $this->tpl->display('guestbook.tpl'); }
displayBook() 方法显示留言簿条目。$data 数组应是留言簿条目的数组。这会分配给模板,然后显示模板。