
|
Smarty Templates |
|
|---|---|
![]() |
|
| 开发 | Monte Ohrt, Messju Mohr |
| 最近版本 | 2.6.20 / 2008年8月15日 |
| 类型 | 网页模板引擎 |
| 许可协议 | LGPL |
| 网站 | smarty.net |
|
查看 • 讨论 • 编辑 • 历史
|
|
Smarty 是一个PHP下的网页模板系统。 Smarty基本上是一种为了将不同考量的事情分离而推出的工具,这对某些应用程式是一种共通性设计策略。[1][2]
目录 |
Smarty 以在文件中放置特殊的“Smarty标签”来产生网页内容。这些标签会被处理并替换成其他的内容。
标签是给Smarty的指令符,以模板定界符包住。这些指令符可以是变量,以$符号代表函数、逻辑 或 流程控制 语法. Smarty 允许 PHP 程式设计师以Smarty 标签去定义可存取的函数。
Smarty 意图简化区域化,允许PHP网页的使用者接口分开会改变的部份与后端逻辑。 理想的情况下,这将降低软件维护费用和人力。 在这个研发策略之下, 设计师被保护免于不用撰写使用者接口的程式码,并且PHP程式设计师被于不断的重复撰写使用者接口的程式码。
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>
Why are we here?
All text is available under the terms of the GNU Free Documentation License
This page is cache of Wikipedia. History