Chapter 1. What is Smarty?

Smarty is a template engine for PHP. More specifically, it facilitates a manageable way to separate application logic and content from its presentation. This is best described in a situation where the application programmer and the template designer play different roles, or in most cases are not the same person.

Smarty 是一個 PHP 的樣板引擎,他提供了一個可管理的方式,讓使用者可以依照自 己的意思將商業邏輯、內容與呈現方法分開。意思是程式設計人員與樣本設計人員可以自己做自 己的事情,而不必一個人作多種事情。

For example, let's say you are creating a web page that is displaying a newspaper article. The article headline, tagline, author and body are content elements, they contain no information about how they will be presented. They are passed into Smarty by the application, then the template designer edits the templates and uses a combination of HTML tags and template tags to format the presentation of these elements (HTML tables, background colors, font sizes, style sheets, etc.) One day the programmer needs to change the way the article content is retrieved (a change in application logic.) This change does not affect the template designer, the content will still arrive in the template exactly the same. Likewise, if the template designer wants to completely redesign the templates, this requires no changes to the application logic. Therefore, the programmer can make changes to the application logic without the need to restructure templates, and the template designer can make changes to templates without breaking application logic.

舉例而言,假設你新設計一個網頁內容是報紙的文章。文章的主標題、附標題、作者 、本文,而這些內容不會知道他們會被怎麼呈現出來。這些內容會被應用程式送到 Smarty,然 後樣板設計人員會開始編輯樣板,運用 HTML 標籤與樣板標籤將這些內容格式化(如 HTML表格 、背景顏色、字型大小、樣式表等等之類的)。假使日後程式設計人員需要改變文章內容的取得 方式的話(應用邏輯的變更),例如此類的改變將不會影響樣板設計人員的工作,對樣板設計人 員而言內容還是以相同的方式到達樣板。同樣的,如果樣板設計人員想重新設計樣板,此類的改 變也將不會影響程式設計人員的應用邏輯。

One design goal of Smarty is the separation of business logic and presentation logic. This means templates can certainly contain logic under the condition that it is for presentation only. Things such as including other templates, altering table row colors, upper-casing a variable, looping over an array of data and displaying it, etc. are all examples of presentation logic. This does not mean that Smarty forces a separation of business and presentation logic. Smarty has no knowledge of which is which, so placing business logic in the template is your own doing. Also, if you desire no logic in your templates you certainly can do so by boiling the content down to text and variables only.

Smarty 的其中一個設計目的在獨立出商業邏輯與呈現邏輯。這也代表著樣板可以在單 純呈現內容的狀況下包含某些邏輯。此類邏輯包含其他樣板、表格內行與行顏色的變換、將變數 轉成大寫、巡視與呈現陣列內的資料等等之類的。但是這樣並不表示 Smarty 強制分離商業與呈 現邏輯,實際上 Smarty 並無法分辨何謂商業邏輯何謂呈現邏輯,你也可以將商業邏輯放到樣板 內。這樣代表著如果你 不希望 你的樣板內有任何的商業邏輯的話,你也可以用一些文字與變數來設計你的樣板。

One of the unique aspects about Smarty is the template compiling. This means Smarty reads the template files and creates PHP scripts from them. Once they are created, they are executed from then on. Therefore there is no costly template file parsing for each request, and each template can take full advantage of PHP compiler cache solutions such as Zend Accelerator (http://www.zend.com/) or PHP Accelerator (http://www.php-accelerator.co.uk).

Smarty 有一個很特別的特徵就是樣板編輯(template compiling)的功能。樣板編輯 功能的意思是 Smarty 會讀取樣板檔案,然後根據讀取的樣板產出對應的 PHP 程式檔。當 PHP 程式檔產生之後 Smarty 就會執行這些根據樣版檔產生的 PHP 程式檔,也因此就不會為 了完成客戶端的請求連線而須要費時的樣板編譯,如此一來每份樣版也可以善用 PHP 編譯器 快取解決方案的優點了,如 Zend Accelerator (http://www.zend.com/) 或 PHP Accelerator (http://www.php-accelerator.co.uk)。

Some of Smarty's features:

Smarty 的特性: