Blame | Letzte Änderung | Log anzeigen | RSS feed
Features=======Some of Smarty's features:- It is extremely fast.- It is efficient since the PHP parser does the dirty work.- No template parsing overhead, only compiles once.- It is smart about [recompiling](#variable.compile.check) only thetemplate files that have changed.- You can easily create your own custom[functions](#language.custom.functions) and [variablemodifiers](#language.modifiers), so the template language isextremely extensible.- Configurable template [{delimiter}](#variable.left.delimiter) tagsyntax, so you can use `{$foo}`, `{{$foo}}`, `<!--{$foo}-->`, etc.- The [`{if}..{elseif}..{else}..{/if}`](#language.function.if)constructs are passed to the PHP parser, so the `{if...}` expressionsyntax can be as simple or as complex an evaluation as you like.- Allows unlimited nesting of[`sections`](#language.function.section), `if's` etc.- Built-in [caching](#caching) support- Arbitrary [template](#resources) sources- [Template Inheritance](#advanced.features.template.inheritance) foreasy management of template content.- [Plugin](#plugins) architecture## Separation of presentation from application code- This means templates can certainly contain logic under the conditionthat it is for presentation only. Things such as[including](./designers/language-builtin-functions/language-function-include.md) other templates,[alternating](./designers/language-custom-functions/language-function-cycle.md) table row colors,[upper-casing](./designers/language-modifiers/language-modifier-upper.md) a variable,[looping](./designers/language-builtin-functions/language-function-foreach.md) over an array of data andrendering it are examples of presentation logic.- This does not mean however that Smarty forces a separation ofbusiness and presentation logic. Smarty has no knowledge of which iswhich, so placing business logic in the template is your own doing.- Also, if you desire *no* logic in your templates you certainly cando so by boiling the content down to text and variables only.## How does it work?Under the hood, Smarty "compiles" (basically copies and converts) thetemplates into PHP scripts. This happens once when each template isfirst invoked, and then the compiled versions are used from that pointforward. Smarty takes care of this for you, so the template designerjust edits the Smarty templates and never has to manage the compiledversions. This approach keeps the templates easy to maintain, and yetkeeps execution times extremely fast since the compiled code is justPHP. And of course, all PHP scripts take advantage of PHP op-code cachessuch as APC.## Template InheritanceTemplate inheritance was introduced in Smarty 3. Before templateinheritance, we managed our templates inpieces such as header and footer templates. This organization lendsitself to many problems that require some hoop-jumping, such as managingcontent within the header/footer on a per-page basis. With templateinheritance, instead of including other templates we maintain ourtemplates as single pages. We can then manipulate blocks of contentwithin by inheriting them. This makes templates intuitive, efficient andeasy to manage. See[Template Inheritance](./programmers/advanced-features/advanced-features-template-inheritance.md)for more info.## Why not use XML/XSLT syntax?There are a couple of good reasons. First, Smarty can be used for morethan just XML/HTML based templates, such as generating emails,javascript, CSV, and PDF documents. Second, XML/XSLT syntax is even moreverbose and fragile than PHP code! It is perfect for computers, buthorrible for humans. Smarty is about being easy to read, understand andmaintain.## Template SecurityAlthough Smarty insulates you from PHP, you still have the option to useit in certain ways if you wish. Template security forces the restrictionof PHP (and select Smarty functions.) This is useful if you have thirdparties editing templates, and you don't want to unleash the full powerof PHP or Smarty to them.## IntegrationSometimes 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 caneasily be integrated as the view layer of an MVC. Many of the morepopular ones have integration instructions for Smarty, or you may findsome help here in the forums and documentation.## Other Template EnginesSmarty is not the only engine following the *"Separate Programming Codefrom Presentation"* philosophy. For instance, Python has templateengines built around the same principles such as Django Templates andCheetahTemplate. *Note: Languages such as Python do not mix with HTMLnatively, which give them the advantage of proper programming codeseparation from the outset. There are libraries available to mix Pythonwith HTML, but they are typically avoided.*## What Smarty is NotSmarty is not an application development framework. Smarty is not anMVC. Smarty is not an alternative to Laravel, Symfony, CodeIgniter,or any of the other application development frameworks for PHP.Smarty is a template engine, and works as the (V)iew component of yourapplication. Smarty can easily be coupled to any of the engines listedabove as the view component. No different than any other software,Smarty has a learning curve. Smarty does not guarantee good applicationdesign or proper separation of presentation, this still needs to beaddressed by a competent developer and web designer.## Is Smarty Right for Me?Smarty is not meant to be a tool for every job. The important thing isto identify if Smarty fits your needs. There are some importantquestions to ask yourself:### Template SyntaxAre you content with PHP tags mixed with HTML? Are yourweb designers comfortable with PHP? Would your web designers prefer atag-based syntax designed for presentation? Some experience working withboth Smarty and PHP helps answer these questions.### The Business CaseIs there a requirement to insulate the templates fromPHP? Do you have untrusted parties editing templates that you do notwish to unleash the power of PHP to? Do you need to programmaticallycontrol what is and is not available within the templates? Smartysupplies these capabilities by design.## Feature setDoes Smarty's features such as caching, templateinheritance and plugin architecture save development cycles writing codethat would be needed otherwise? Does the codebase or framework you planon using have the features you need for the presentation component?## Sites using SmartyMany well-known PHP projects make use of Smarty such as XOOPS CMS, CMS Made Simple, TikiCMS/Groupware and X-Cart to name a few.## SummaryWhether you are using Smarty for a small website or massive enterprisesolution, it can accommodate your needs. There are numerous featuresthat 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