Revision 148 | Blame | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed
# Philosophy## What is Smarty?Smarty is a template engine for PHP. More specifically, it facilitates amanageable way to separate application logic and content from itspresentation. This is best described in a situation where theapplication 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 anewspaper article.- The article `$headline`, `$tagline`, `$author` and `$body` arecontent elements, they contain no information about how they will bepresented. They are [passed](#api.assign) into Smarty by theapplication.- Then the template designer edits the templates and uses acombination of HTML tags and [template tags](#language.basic.syntax)to format the presentation of these[variables](#language.syntax.variables) with elements such astables, div\'s, background colors, font sizes, style sheets, svgetc.- One day the programmer needs to change the way the article contentis retrieved, ie a change in application logic. This change does notaffect the template designer, the content will still arrive in thetemplate exactly the same.- Likewise, if the template designer wants to completely redesign thetemplates, this would require no change to the application logic.- Therefore, the programmer can make changes to the application logicwithout the need to restructure templates, and the template designercan make changes to templates without breaking application logic.## GoalsThe Smarty design was largely driven by these goals:- clean separation of presentation from application code- PHP backend, Smarty template frontend- complement 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## Two camps of thoughtWhen it comes to templating in PHP, there are basically two camps ofthought. The first camp exclaims that \"PHP is a template engine\". Thisapproach simply mixes PHP code with HTML. Although this approach isfastest from a pure script-execution point of view, many would arguethat the PHP syntax is messy and complicated when mixed with taggedmarkup such as HTML.The second camp exclaims that presentation should be void of allprogramming code, and instead use simple tags to indicate whereapplication content is revealed. This approach is common with othertemplate engines (even in other programming languages), and is also theapproach that Smarty takes. The idea is to keep the templates focusedsquarely on presentation, void of application code, and with as littleoverhead 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 degenerateswhen mixed with HTML. Smarty\'s simple {tag} syntax is designedspecifically to express presentation. Smarty focuses your templateson presentation and less on \"code\". This lends to quicker templatedeployment and easier maintenance. Smarty syntax requires no workingknowledge of PHP, and is intuitive for programmers andnon-programmers alike.- INSULATION: When PHP is mixed with templates, there are norestrictions on what type of logic can be injected into a template.Smarty insulates the templates from PHP, creating a controlledseparation of presentation from business logic. Smarty also hassecurity features that can further enforce restrictions ontemplates.## Web designers and PHPA common question: "Web designers have to learn a syntax anyway, whynot PHP?" Of course web designers can learn PHP, and they may alreadybe familiar with it. The issue isn't their ability to learn PHP, it isabout the consequences of mixing PHP with HTML. If designers use PHP, itis too easy to add code into templates that doesn't belong there (youjust handed them a swiss-army knife when they just needed a knife.) Youcan teach them the rules of application design, but this is probablysomething they don't really need to learn (now they are developers!)The PHP manual is also an overwhelming pile of information to siftthrough. It is like handing the owner of a car the factory assemblymanual when all they need is the owners manual. Smarty gives webdesigners exactly the tools they need, and gives developers fine-grainedcontrol over those tools. The simplicity of the tag-based syntax is alsoa huge welcome for designers, it helps them streamline the organizationand management of templates.