PHP Template
工讀生 機械所 劉立業
Outline
Web Template
Web Template Classification
Introduction to Smarty
Smarty Usage
Smarty Example
References
Web Template
Separate content from
presentation on the web
design.
Suitable for websites
often require regular
content updates, and
standardization of
appearance.
*Picture refer from wikipedia
Web Template Classification
(1/3)
Static web template (Outside server
template system architecture)
e.g. Dreamweaver, FrontPage
*Picture refer from wikipedia
Web Template Classification
(2/3)
Server-side web template (Server-side
template system architecture)
e.g. SSI(Server Side Includes),Smarty
base: ASP,JSP,PHP,Perl
*Picture refer from wikipedia
Web Template Classification
(3/3)
Client-side web template (Distributed
template system architecture)
e.g. Ajax,RIA
*Picture refer from wikipedia
(Rich Internet application: JavaScript,Flash Player,ActiveX Controls,Java applets,SVG)
Introduction to Smarty
Smarty is a server-side template system.
Separate application logic and business logic
from presentation logic
– Designers can't break application code, and the code
will be tighter, more secure and easier to maintain.
– Errors in the templates are as simple and intuitive as
possible for the designer.
– Designers can modify or completely redesign without
intervention from the programmer.
– Programmers can maintain the application code
without disturbing the presentation layer.
Smarty Usage (1/2)
Step 1. 載入 Smarty 樣版引擎
include "Smarty.class.php";
Step 2. 建立 Smarty 物件
$tpl = new Smarty();
Step 3. 設定 Smarty 物件的參數
$tpl->template_dir = “xxx”;
$tpl->compile_dir (此目錄屬性須設定為777或去除唯讀)
$tpl->config_dir
$tpl->cache_dir
Step 4. 在程式中處理變數後,再用 Smarty 的 assign 方法將變數
置入樣版裡
$tpl->assign(“變數名稱”, 值);
Step 5. 利用 Smarty 的 display 方法將網頁秀出
$tpl->display('test.tpl');
Smarty Usage (2/2)
template_dir = __SITE_ROOT . "/templates/";
$tpl->compile_dir = __SITE_ROOT . "/templates_c/";
$tpl->config_dir = __SITE_ROOT . "/configs/";
$tpl->cache_dir = __SITE_ROOT . "/cache/";
?>
Smarty Example (1/3)
{$title}
{$content}
Smarty Example (2/3)
assign("title", "測試用的網頁標題");
$tpl->assign("content", "測試用的網頁內容");
$tpl->display('test.tpl');
?>
Smarty Example (3/3)
templates_c/%%179/%%1798044067/test.tpl.php:
_tpl_vars['title']; ?>
_tpl_vars['content']; ?>
References
Wikipedia (List Keyword Only)
– Web template
– Web template system
– Template engine
– Rich Internet application
Smarty Home
– http://smarty.php.net/
Smarty入門(中文)
– http://www.php5.idv.tw/documents/smarty/smarty20040209.htm
SmartyWiki
– http://smarty.incutio.com/