<table cellspacing="0" cellpadding="0" width="100%" summary="Header navigation table" border="0"><tbody><tr><th align="middle" colspan="3">Smarty - the compiling PHP template engine</th></tr><tr><td valign="bottom" align="left" width="25%"><a accesskey="P" href="getting.started.html">Prev</a></td> <td valign="bottom" align="middle" width="50%"/> <td valign="bottom" align="right" width="25%"><a accesskey="N" href="installation.html">Next</a></td></tr></tbody></table>
# Chapter 1. What is Smarty?
第一章.什么是Smaty?
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.
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 variables with elements such as tables, div's, background colors, font sizes, style sheets, svg etc.
* One day the programmer needs to change the way the article content is retrieved, ie 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 would require no change 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.
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, alternating table row colors, upper-casing a variable, looping over an array of data and displaying it are examples of presentation logic.
* This does not mean however 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是一個php模板引擎。更準確的說,它分開了邏輯程序和外在的內容,提供了一種易于管理的方法。可以描述為應用程序員和美工扮演了不同的角色,因為在大多數情況下,他們不可能是同一個人。
例如,你正在創建一個用于瀏覽新聞的網頁。
* 新聞標題、標簽欄、作者和內容等變量都是內容要素,他們并不包含應該怎樣呈現的信息。而這些正是Smarty要做的。
* 模板設計者們編輯模板,組合使用html標簽和模板標簽去格式化這些要素的輸出(html表格,背景色,字體大小,樣式表,等等)。
* 有一天程序員想要改變文章檢索的方式(也就是程序邏輯的改變)。這個改變不影響模板設計者,內容仍將準確的輸出到模板。
* 同樣的,哪天美工吃多了想要完全重做界面,也不會影響到程序邏輯。
* 因此,程序員可以改變邏輯而不需要重新構建模板,模板設計者可以改變模板而不影響到邏輯。
Smarty總的設計理念就是分離業務邏輯和表現邏輯。
* 這意味著模版在只為表現外在內容而存在的前提下必定包含邏輯。諸如載入其它模版、交替顯示表格行顏色、大寫變量、循環數據數組并將其顯示出來,此謂模版的表現邏輯的例子。
* 但這并不意味著Smarty強制分離業務邏輯和表現邏輯。Smarty不具備分清誰是誰的能力,因此只要你高興,你照樣可以將業務邏輯放入模版。
* 此外,如果你希望在模版中不包含邏輯,你當然可以只將內容加工為文本和變量。
Some of Smarty's features:
Smaty的一些特點:
-
It is extremely fast.
非常非常的快!
-
It is efficient since the PHP parser does the dirty work.
用php分析器干這個苦差事是有效的
-
No template parsing overhead, only compiles once.
不需要多余的模板語法解析,僅僅是編譯一次
-
It is smart about recompiling only the template files that have changed.
僅對修改過的模板文件進行重新編譯
-
You can make [custom functions](#) and custom [variable modifiers](#), so the template language is extremely extensible.
可以編輯'自定義函數'和自定義'變量',因此這種模板語言完全可以擴展
-
Configurable template delimiter tag syntax, so you can use {}, {{}}, <!--{}-->, etc.
可以自行設置模板定界符,所以你可以使用{}, {{}}, <!--{}-->, 等等
-
The if/elseif/else/endif constructs are passed to the PHP parser, so the {if ...} expression syntax can be as simple or as complex as you like.
諸如 if/elseif/else/endif 語句可以被傳遞到php語法解析器,所以 {if ...} 表達式是簡單的或者是復合的,隨你喜歡啦
- Allows unlimited nesting of sections, if's etc.
可以無限嵌套sections、if等。
-
Built-in caching support
內建緩存支持
-
Arbitrary template sources
獨立模板文件
-
Custom cache handling functions
可自定義緩存處理函數
- Template Inheritance for easy management of template content.
模板繼承易于管理模板內容
-
Plugin architecture
插件體系結構
<table cellspacing="0" cellpadding="0" width="100%" summary="Footer navigation table" border="0"><tbody><tr><td valign="top" align="left" width="33%"><a accesskey="P" href="getting.started.html">Prev</a></td> <td valign="top" align="middle" width="34%"><a accesskey="H" href="index.html">Home</a></td> <td valign="top" align="right" width="33%"><a accesskey="N" href="installation.html">Next</a></td></tr><tr><td valign="top" align="left" width="33%">Getting Started<br/> 開始</td> <td valign="top" align="middle" width="34%"><a accesskey="U" href="getting.started.html">Up</a></td> <td valign="top" align="right" width="33%">Installation<br/> 安裝</td></tr></tbody></table>
- Smarty模板編譯引擎
- 序
- 譯序
- I.開始
- 第一章. 什么是Smarty?
- 第二章. 安裝
- II.模板設計者篇
- 第三章.基本語法
- 注釋
- 變量
- 函數
- 屬性
- 雙引號里嵌入變量
- 數學運算
- 忽略Smarty解析
- 第四章.變量
- 從PHP分配的變量
- 從配置文件讀取的變量
- 變量范圍
- {$smarty}保留變量
- 第五章.變量調節器
- capitalize
- cat
- count_characters
- count_paragraphs
- count_sentences
- count_words
- date_format
- default
- escape
- indent
- lower
- nl2br
- regex_replace
- replace
- spacify
- string_format
- strip
- strip_tags
- truncate
- upper
- wordwrap
- 第六章.組合修改器
- 第七章.內置函數
- {$var=}
- {append}
- {assign}
- {block}
- {call}
- {capture}
- {config_load}
- {debug}
- {extends}
- {for}
- {foreach},{foreachelse}
- @index
- {function}
- {if},{elseif},{else}
- {include}
- {include_php}
- {insert}
- {ldelim},{rdelim}
- {literal}
- {nocache}
- {php}
- {section},{sectionelse}
- .index
- {while}
- 第八章.自定義函數
- {counter}
- {cycle}
- {eval}
- {fetch}
- {html_checkboxes}
- {html_image}
- {html_options}
- {html_radios}
- {html_select_date}
- {html_select_time}
- {html_table}
- {mailto}
- {math}
- {textformat}
- 第九章.配置文件
- 第十章.調試控制臺
- III.模板程序員篇
- 第十一章 常量
- SMARTY_DIR
- 第十二章 Smarty類變量
- $template_dir
- 第十三章.Smarty類方法
- append()
- appendByRef()
- assign()
- assignByRef()
- clearAllAssign()
- clearAllCache()
- clearAssign()
- clearCache()
- clearCompiledTpl()
- clearConfig()
- compileAllConfig()
- compileAllTemplates()
- configLoad()
- createData()
- createTemplate()
- disableSecurity()
- display()
- enableSecurity()
- fetch()
- getConfigVars()
- getRegisteredObject()
- getTags()
- getTemplateVars()
- isCached()
- loadFilter()
- registerFilter()
- registerPlugin()
- registerObject()
- registerResource()
- templateExists()
- unregisterFilter()
- unregisterPlugin()
- unregisterObject()
- unregisterResource()
- testInstall()
- 第十四章.緩存
- 建立緩存
- 多重緩存
- 緩存集合
- 控制插件輸出的可緩存性
- 第十五章.高級特性
- 安全
- 通過模板更改設置
- 模板繼承
- 數據流
- 對象
- 靜態類
- 預過濾器
- 后過濾器
- 輸出過濾器
- 緩存處理函數
- 資源
- 第十六章.以插件擴展Smarty
- 插件如何工作
- 命名約定
- 編寫插件
- 模板函數
- 調節器
- 塊函數
- 編譯函數
- 預濾器/后濾器
- 輸出過濾器
- 資源
- 插入
- Ⅳ.附錄
- 第十七章.疑難解答
- Smarty/PHP 錯誤
- 第十八章.使用技巧和經驗
- 空白變量處理
- 默認變量處理
- 傳遞變量標題給頭模板
- 日期
- WAP/WML
- 組件化模板
- 拒絕電子郵件地址
- 第十九章. 相關資源
- 第二十章. 漏洞
- 3.0安裝包
- 2.x版本升級至3.x版本的提示
- 3.0.x使用指南
- 翻譯人員列表