Smarty
维库,知识与思想的自由文库
3个分类: 翻譯請求 | Template engines | 来自中文维基百科
| Smarty | |
![]() |
|
| 开发者 | Monte Ohrt, Messju Mohr |
|---|---|
| 最新穩定版 | 2.6.18 / 7th Mar 2007 |
| 类型 | 模板引擎 |
| 许可协议 | LGPL |
| smarty.php.net | |
Smarty 是一個PHP下的網頁模板系統。 Smarty is primarily promoted as a tool for separation of concerns, which is a common design strategy for certain kinds of applications.[1][2]
Smarty generates web content by the placement of 特別的 Smarty 標籤 within 一個文件。那些標籤被處理與替換成其他的內容。
Tags are directives for Smarty that are enclosed by template delimiters. These directives can be variables, denoted by a dollar sign ($), 函數, 或 邏輯 或 流程控制 statements. Smarty 允許 PHP 程式設計師去定義函數 that can be accessed using Smarty tags.
Smarty 意圖簡化 compartmentalization, allowing the presentation of a web page to change separately from the back-end. Ideally, this eases the costs and efforts associated with software maintenance. Under successful application of this development strategy, designers are shielded from the back-end coding, and PHP programmers are shielded from the presentation coding.
Smarty 支援幾個高階模板程式的特色,包含:
- 正規表示法
- Control flow statements, foreach, while
- if, elseif, else
- 可修改的變數 - 例如 {$variable|nl2br}
- 使用者自訂的函數
- 在模板內的數學計算
along with other features. There are other template engines that also support these features.
目录 |
[编辑] 程式碼範例
- 因為 Smarty 從 HTML 分離出 PHP,所以你有兩個檔案:
<source lang="html4strict"> <!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 *}
{$body_text}
</body> </html> </source>
- 在企業邏輯程式碼中,你您能配置 Smarty 去使用這個模板:
<source lang="php"> 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'); </source>
- 我們並且能包括 PHP 在 Smarty 模板裡,像是下面這樣:
<source lang="php"> {* 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> </source>
[编辑] 參照
- ↑ Smarty separates PHP code, (often represented as business logic) from HTML, (often represented as presentation logic).
- ↑ Parr, Terence John (2004). Enforcing strict model-view separation in template engines. Proceedings of the 13th international conference on World Wide Web. 1-58113-844-X.
[编辑] 參見
[编辑] 外部連結
[编辑] 英文
- Official site
- PHP Templating with Smarty by Cezar Floroiu - Smarty tutorial
- Smarty vs. XML/XSLT - from DevPapers.com by Sergey Makogon
- Smarty Cheat Sheet Smarty Cheat Sheet for Templates Designers and Programmers
- Timestretch: PHP, MySQL, and Smarty Programming - Also see the PHP2 page for more.
- PHP Smarty Tools - Using Smarty in PhpED





