?
Ce document utilise Manuel du site Web PHP chinois Libérer
第一章.什么是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 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是一個(gè)php模板引擎。更準(zhǔn)確的說,它分開了邏輯程序和外在的內(nèi)容,提供了一種易于管理的方法??梢悦枋鰹閼?yīng)用程序員和美工扮演了不同的角色,因?yàn)樵诖蠖鄶?shù)情況下 ,他們不可能是同一個(gè)人。例如,你正在創(chuàng)建一個(gè)用于瀏覽新聞的網(wǎng)頁(yè),新聞標(biāo)題,標(biāo)簽欄,作者和內(nèi)容等都是內(nèi)容要素,他們并不包含應(yīng)該怎樣去呈現(xiàn)。在Smarty的程序里,這些被忽略了。模板設(shè)計(jì)者們編輯模板,組合使用html標(biāo)簽和模板標(biāo)簽去格式化這些要素的輸出(html表格,背景色,字體大小,樣式表,等等)。有一天程序員想要改變文章檢索的方式(也就是程序邏輯的改變)。這個(gè)改變不影響模板設(shè)計(jì)者,內(nèi)容仍將準(zhǔn)確的輸出到模板。同樣的,哪天美工吃多了想要完全重做界面,也不會(huì)影響到程序邏輯。因此,程序員可以改變邏輯而不需要重新構(gòu)建模板,模板設(shè)計(jì)者可以改變模板而不影響到邏輯。
Now for a short word on what Smarty does NOT do. Smarty does not attempt to completely separate logic from the templates. There is no problem with logic in your templates under the condition that this logic is strictly for presentation. A word of advice: keep application logic out of the templates, and presentation logic out of the application. This will most definately keep things manageable and scalable for the foreseeable future.
現(xiàn)在簡(jiǎn)短的說一下什么是smarty不做的。smarty不嘗試將邏輯完全和模板分開。如果邏輯程序嚴(yán)格的用于頁(yè)面表現(xiàn),那么它在模板里不會(huì)出現(xiàn)問題。有個(gè)建議:讓應(yīng)用程序邏輯遠(yuǎn)離模板, 頁(yè)面表現(xiàn)邏輯遠(yuǎn)離應(yīng)用程序邏輯。這將在以后使內(nèi)容更容易管理,程序更容易升級(jí)。
One of the unique aspects about Smarty is the template compling. 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的特點(diǎn)之一是"模板編譯"。意思是Smarty讀取模板文件然后用他們創(chuàng)建php腳本。這些腳本創(chuàng)建以后將被執(zhí)行。因此并沒有花費(fèi)模板文件的語(yǔ)法解析,同時(shí)每個(gè)模板可以享受到諸如Zend加速器(http://www.zend.com) 或者PHP加速器(http://www.php-accelerator.co.uk)。這樣的php編譯器高速緩存解決方案。
Some of Smarty's features:
Smaty的一些特點(diǎn):
It is extremely fast.
非常非常的快!
It is efficient since the PHP parser does the dirty work.
用php分析器干這個(gè)苦差事是有效的
No template parsing overhead, only compiles once.
不需要多余的模板語(yǔ)法解析,僅僅是編譯一次
It is smart about recompiling only the template files that have
changed.
僅對(duì)修改過的模板文件進(jìn)行重新編譯
You can make custom
functions and custom variable
modifiers, so the template language is extremely extensible.
可以編輯'自定義函數(shù)'和自定義'變量',因此這種模板語(yǔ)言完全可以擴(kuò)展
Configurable template delimiter tag syntax, so you can use {}, {{}},
<!--{}-->, etc.
可以自行設(shè)置模板定界符,所以你可以使用{}, {{}},
<!--{}-->, 等等
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 語(yǔ)句可以被傳遞到php語(yǔ)法解析器,所以 {if ...} 表達(dá)式是簡(jiǎn)單的或者是復(fù)合的,隨你喜歡啦
Unlimited nesting of sections, ifs, etc. allowed.
如果允許的話,section之間可以無(wú)限嵌套
It is possible to embed PHP code right in your template files, although
this may not be needed (nor recommended) since the engine is so
customizable.
引擎是可以定制的.可以內(nèi)嵌php代碼到你的模板文件中,雖然這可能并不需要(不推薦)
Built-in caching support
內(nèi)建緩存支持
Arbitrary template sources
獨(dú)立模板文件
Custom cache handling functions
可自定義緩存處理函數(shù)
Plugin architecture
插件體系結(jié)構(gòu)