Smarty

程式码范例

因为 Smarty将 HTML码与PHP程式码分离，所以会有两个档案：
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html><head>
   <title>{$title_text}</title>
   <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /></head><body> {* This is a little comment that won't be visible in the HTML source *}

<p>{$body_text}</p></body><!-- this is a little comment that will be seen in the HTML source --></html>
在企业逻辑程式码中，设计者能配置Smarty去使用这个模板：
define('SMARTY_DIR', 'smarty-2.6.9/' );require_once(SMARTY_DIR . 'Smarty.class.php');$smarty = new Smarty();$smarty->template_dir = './templates/';$smarty->compile_dir = './templates/compile/';$smarty->cache_dir = './templates/cache/';$smarty->caching = false;$smarty->error_reporting = E_ALL; // LEAVE E_ALL DURING DEVELOPMENT$smarty->debugging = true;$smarty->assign('title_text', 'TITLE: This is the Smarty basic example ...');$smarty->assign('body_text', 'BODY: This is the message set using assign()');$smarty->display('index.tpl');
并且能将PHP包涵在Smarty模板里，像是下面这样：
{* We can use PHP syntax in smarty template inside {php} ..{/php} *}{php} 
$contentType = strpos($_SERVER['HTTP_ACCEPT']，'application/xhtml+xml') === false ? 'text/html' : 'application/xhtml+xml';header("Content-Type: $contentType; charset=utf-8"); 
{/php}<html><head><title>{$page_title}</title></head><body>
   {$body_text}</body></html>