<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<br/><br/> Smarty - PHP 模板编译引擎</th></tr><tr><td valign="bottom" align="left" width="25%"><a accesskey="P" href="index.html">Prev</a></td> <td valign="bottom" align="middle" width="50%"/> <td valign="bottom" align="right" width="25%"><a accesskey="N" href="opinion.html">Next</a></td></tr></tbody></table> # Preface 序 **The Philosophy** The Smarty design was largely driven by these goals: ◆ clean separation of presentation from application code ◆ PHP backend, Smarty template frontend ◆ compliment PHP, not replace it ◆ fast development/deployment for programmers and designers ◆ quick and easy to maintain ◆ syntax easy to understand, no PHP knowledge necessary ◆ flexibility for custom development ◆ security: insulation from PHP ◆ free, open source **What is Smarty? **Smarty is PHP a framework for separating presentation (HTML/CSS) from application logic (PHP). This implies that PHP code is application logic, and is separated from the presentation. **Two camps of thought **When it comes to templating in PHP, there are basically two camps of thought. The first camp exclaims that "PHP is a template engine". Simply because PHP can mix with presentation doesn't mean it's a preferable choice. The only virtue to this approach is speed from a pure script-execution point of view. However, the PHP syntax is quite ugly and doesn't mix well with tagged markup such as HTML. The second camp exclaims that presentation should be void of all programming code, and instead use simple tags to indicate where application content is revealed. This approach is common with other development languages, and is also the approach that Smarty takes. The idea is to keep the templates focused squarely on presentation, not application code, and with as little overhead as possible. **Why is separating PHP from templates important? **Two major benefits: ◆ SYNTAX: Templates typically consist of semantic markup such as HTML. PHP syntax works well for application code, but quickly degenerates when mixed with HTML. Smarty's simple {tag} syntax is designed specifically to express presentation.Smarty focuses your templates on presentation and less on "code". This lends to quicker template deployment and easier maintenance. Smarty syntax requires no working knowledge of PHP, and is intuitive for programmers and non-programmers alike. ◆ INSULATION: When PHP is mixed with templates, there are no restrictions on what type of logic can be injected into atemplate. Smarty insulates the templates from PHP, creating a controlled separation of presentation from business logic.Smarty also has security features that can further enforce restrictions on templates. **Web designers and PHP ** A common question: "Web designers have to learn a syntax anyways, why not PHP?". Of course web designers can learn PHP, but this isn't about their ability to learn it. They learn PHP, then start stuffing things into templates that don't belong there (you just handed them a swiss-army knife when they just needed a knife.) You can teach them the rules of application development, but then you may as well call them developers. Smarty gives web designers exactly the tools they need, and gives developers fine-grained control over those tools. **Implementation is Important ** A common problem with Smarty development is poor implementation. If you abuse Smarty fundamentals or come across an application that does (i.e. injecting PHP in templates), you may end up with something worse than what Smarty was intended to resolve. See the Best Practices section on the Smarty website for examples of what to look out for. **How does it work? ** Under the hood, Smarty "compiles" (basically copies and converts) the templates into PHP scripts. This happens once when each template is first invoked, and then the compiled versions are used from that point forward. Smarty takes care of this for you, so the template designer just edits the Smarty templates and never has to manage the compiled versions. This approach keeps the templates easy to maintain, and yet keeps execution times extremely fast since the compiled code is just PHP. And of course, all PHP scripts take advantage of PHP op-code caches such as APC. **Template Inheritance ** Template inheritance is new to Smarty 3, and it's one of many great new features. Before template inheritance, we managed our templates in pieces such as header and footer templates. This organization lends itself to many problems that require some hoop-jumping, such as managing content within the header/footer on a per-page basis. With template inheritance, instead of including other templates we maintain our templates as single pages. We can then manipulate blocks of content within by inheriting them. This makes templates intuitive, efficient and easy to manage. See the Template Inheritance section of the Smarty website for more info. **Why not use XML/XSLT syntax? ** There are a couple of good reasons. First, Smarty can be used for more than just XML/HTML based templates, such as generating emails, javascript, CSV, and PDF documents. Second, XML/XSLT syntax is even more verbose and fragile than PHP code! It is perfect for computers, but horrible for humans. Smarty is about being easy to read, understand and maintain. **Template Security ** Although Smarty insulates you from PHP, you still have the option to use it in certain ways if you wish.Template security forces the restriction of PHP (and select Smarty functions.) This is useful if you have third parties editing templates, and you don't want to unleash the full power of PHP or Smarty to them. **Integration 整合** Sometimes Smarty gets compared to Model-View-Controller (MVC) frameworks. Smarty is not an MVC,it is just the presentation layer, much like the View (V) part of an MVC. As a matter of fact, Smarty can easily be integrated as the view layer of an MVC. Many of the more popular ones have integration instructions for Smarty, or you may find some help here in the forums and documentation. **Other Template Engines ** Smarty is not the only engine following the "Separate Programming Code from Presentation" philosophy.For instance, some Python developers decided that mixing Python with HTML/CSS wasn't such a great idea, and built some template engines around the same principles such as Django Templates and CheetahTemplate. **What Smarty is Not Smarty不是什么** Smarty is not meant to be a stand-alone tool for developing websites from scratch. Smarty is a tool to facilitate the presentation layer of your application. You may decide to use an existing framework with Smarty, or you might build your own codebase and use Smarty for the frontend. Either way, Smarty is a great way to decouple presentation from your application code. **Summary ** Whether you are using Smarty for a small website or massive enterprise solution, it can accommodate your needs. There are umerous features that make Smarty a great choice: ◆ separation of PHP from HTML/CSS just makes sense ◆ readability for organization and management ◆ security for 3rd party template access ◆ feature completeness, and easily extendable to your own needs ◆ massive user base, Smarty is here to stay ◆ LGPL license for commercial use ◆ 100% free to use, open source project **Should I use Smarty? ** Smarty's long track record has indicated that it significantly improves the speed of deployment and maintenance of templates. If maintenance is crucial to your application (or business), Smarty is certainly a good fit.However, Smarty is not a magic bullet. It is a tool for managing presentation. Using Smarty, another template engine, or plain PHP is largely going to be determined by your own requirements and tastes. It is certainly possible to maintain presentation mixed with PHP if you are happy with that. You may not have a need for Smarty if you don't need cleaner template syntax, template inheritance, caching, plugins,security (i.e. insulation from PHP), quicker presentational deployment and easier maintenance. The best approach is to ask lots of questions, install and test Smarty for yourself and make an informed decision for your project. **设计理念** Smarty主要为这些目标所驱动设计: ◆ 抛弃应用程序中php与其它语言杂揉的描述方式,使之统一样式 ◆ php负责后台,Smarty模版负责前端 ◆ 向php致意,而不是取代它 ◆ 程序员、美工能够快速开发部署 ◆ 快速和易于维护 ◆ 语法简单、容易理解,不必具备php知识 ◆ 客户在开发中富有弹性 ◆ 安全:从php独立出来 ◆ 免费,开源 ** Smarty是什么?** Smarty是一种从程序逻辑层(php)抽出外在(html/css)描述的PHP框架,这意味着php代码只负责逻辑应用,从外在描述中分离了出来。 ** 两种阵营的思考** 当提到php模版,就存在两种阵营的思想。第一阵营的人大声叫道“php本身就是一种模版引擎”。仅仅因为PHP能混合描述并不意味着这是个好的选择,唯一的优点是纯粹的脚本运行速度快。然而,php语法非常丑陋,根本不能与诸如html的标签语法结合得很好。 第二阵营的人叫道,所有程序代码(包括php等)不应描述外在表现,取而代之的是用简单标签指明在哪里显示应用程序的内容,这种做法在其它语言中很常见,这也是Smarty所推崇的。它(Smarty)的思想是让模版直接专注于外在表现,而不是应用程序,同时保持尽量少的开销。 ** 为什么从模版中分离php很重要?** 两个主要好处: ◆ **语法:**模版由像html一样的语义标记构成。php语法在应用程序中运行得很好,但一旦与html结合则迅速恶化。Smarty简单的{tag}语法专门为外在描述而设计。Smarty专于模版表现而少于“代码”,这可以加快模版开发同时易于维护。Smarty语法无须懂得php知识,它对程序员与非程序员都是直观的。 ◆ **隔离:**当php与模版混合,就没有了何种逻辑类型可以注入到模版的限制。Smarty从php中独立出了模版,创建了一种从业务逻辑中分离外在表现的控制。Smarty同样具有安全特性,能够进一步加强模版的约束。 ** 网页设计师与php** 一个常见的问题:“网页美工必须学语法,为什么php不用?!”。当然,网页设计师可以学php,但这不意味着他们有这个精力去学。他们学习php,然后往模版中添些不属于他们的东西(如果他们需要的话,你也可以教他们瑞士军刀),你可以教他们开发应用程序的规则,另一层意思说你也可以叫他们开发者。Smarty为网页设计师提供所需的恰当工具,为开发人员提供了这些工具的良好控制。 ** 重在实施** 一个在Smarty开发中存在的常见问题是实践不佳。如果你滥用Smarty的基本面或遇到过这样的应用程序(比如在模版中嵌入php),为了不使问题变得更糟糕,你可能会对Smarty敬而远之。在Smarty网站上找到最佳实践例子参考学习下吧。 ** Smarty是怎么工作的?** 在引擎中,Smarty将模版“编译”(基于复制和转换)成php脚本。它只发生一次---当第一次读取模版的时候,指针前进时调取编译版本,Smarty帮你保管它,因此,模版设计者只须编辑Smarty模版,而不必管理编译版本。这也使得模版很容易维护,而执行速度非常快,因为它只是php。当然,php脚本也利用了诸如APC的缓存。 ** 模版继承** 模版的继承是Smarty3的新事物,它也是诸多伟大新特性之一。在还未实现模版继承之前,我们管理着诸如header、footer这样的模版碎片。这种组织模式适合自身请求一些箍跳(?)的问题,如每个页面页眉/页脚的内容管理。在模版继承里,我们将保持模版作为独立页而不用加载其它页面。我们可以操纵内容块继承它们。这使得模版更直观、更有效和易管理。可以在Smarty网站浏览更多关于模版继承的内容介绍。 ** 为什么不用xml/xslt语法?** 这里有一堆理由。第一,Smarty应用范围广范,除了xml/html基础模版,还可运用于email、javascript、csv和pdf文档;第二,xml/xslt语法比php代码冗长和脆弱,它们对于计算机来说是完美的,但对于人类来说又是极其讨厌的。Smarty易于阅读、理解和维护。 ** 模版安全** 虽然Smarty把你从php中解脱出来,但如果你愿意,仍然可以选择使用它(php)。模版安全强制约束php(须选择Smarty相应函数)在使用第三方库编辑模版时很有用,同时你也不必发动php或Smarty的全部威力解决它们。 ** 整合** 有时,Smarty有点类似MVC模式,但Smarty不是MVC框架,它只是一种描述层,更多地类似如MVC的V部份。事实上,Smarty能够容易地整合到MVC中的视图层(V),很多流行的MVC框架指明整合Smarty,或者你也可以在Smarty论坛中找到一些(此类的)帮助文档。 ** 其它模版引擎** Smarty并不是唯一的遵循“表现与程序代码分离”理念的引擎。举例说,一些Python开发商表示将Python代码与html/css混排不是个好的主意,他们也建立了一些类似的规则,比如Django模板和Cheetah模版。 ** Smarty不是什么** Smarty并不是一个在网站开发中一切从零做起的独立工具。Smarty只是个从应用程序中剥离表现层的工具。你可以使用携带Smarty的已有框架,也可以自己建立代码库,使用Smarty作为前端。不管怎样,Smarty是个从应用程序中抽离出表现层的一个伟大方式。 ** 概述** 不管你用Smarty做个小网站还是建立一个大型企业的解决方案,它都能满足你的要求。Smarty的许多特性决定它是个伟大的选择: ◆ 分离php与html/css代码入人心 ◆ 组织、管理模式清晰 ◆ 为第三方模版实行安全检查 ◆ 功能完整,按需扩展 ◆ 巨量用户基础,非常通用 ◆ 通过LGPL商用许可 ◆ 100%安全,开源工程 ** 我该用Smarty吗?** Smarty的历史纪录表明它能显著地提高了模版的部署和维护速度。如果你看重应用的可维护性,Smarty是个正确的选择。然而,Smarty也不是个魔术弹,它只负责管理剥离的表现层。使用Smarty、另一种模版引擎还是单纯的php主要取决于你的需要和体验。如果你喜欢,也可以维护混合php(html/css)的视图。如果你不需要“干净”的模版语法、模版继承、缓存、插件、安全(也就是从php中独立出来)、快速部署、易于管理,你也可以不用Smarty。最好(决定选择)的方法是大量询问别人或亲自安装、测试Smarty,再为你的项目作出一个明智决定。 <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="index.html">Prev</a></td> <td valign="top" align="center" width="34%"><a accesskey="H" href="index.html">Home</a></td> <td valign="top" align="right" width="33%"><a accesskey="N" href="opinion.html">Next</a></td></tr><tr><td valign="top" align="left" width="33%">Smarty - the compiling PHP template engine<br/> Smarty - PHP 模板编译引擎</td> <td valign="top" align="middle" width="34%"> </td> <td valign="top" align="right" width="33%">Preface to Translation<br/> 译序</td></tr></tbody></table>