首页 | 主题 | 图库 | 问答 | 文摘 | 原创 | 百科

历史 | 地理 | 人物 | 艺术 | 体育 | 科学 | 音乐 | 电影 | 信息技术 | 世界遗产

 开放、中立,源自维基百科

个人工具


用搜狗搜索相关网站  Google Search

Smarty

维库,知识与思想的自由文库

跳转到: 导航, 搜索

Image:03wiki-zn-frontpage-icon.gifSmarty正在翻译。欢迎您积极翻译与修订
目前已翻译0%,原文在en:Smarty



Smarty
Image:smarty-logo.gif
开发者 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 支援幾個高階模板程式的特色,包含:

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>

[编辑] 參照

  1. Smarty separates PHP code, (often represented as business logic) from HTML, (often represented as presentation logic).
  2. 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. 

[编辑] 參見

[编辑] 外部連結

[编辑] 英文

[编辑] 中文

其它语言
AD Links