<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>