Smarty Template Development |
|||||||||||||||||||||||||||||
|
When Shah Net Developers work at a project they always think ahead. In particular, they take into consideration future growth and further development of the project. One of the tools that can ease potential updates and changes is the Smarty template system.
|
|||||||||||||||||||||||||||||
|
What is Smarty? Smarty is a web template system written in PHP. Sometimes it is defined as a template engine, sometimes - as a template framework. Smarty is intended to be a tool for separation of concerns, which is a common design strategy for certain kinds of applications. It means that with its help the application code can be separated from the website presentation. What is it done for? The main purpose is to separate the intervention of a programmer and a designer into the code and to make their work independent. Smarty websites are a lot easier to be adjusted to a new design or another model of presentation. |
|||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
|
Special Features These are just a few features and tools of Smarty that should be mentioned: Caching: Smarty provides fine-grained caching features for caching all or parts of a rendered web page, or leaving parts uncached. Programmers can register template functions as cacheable or non-cachable, group cached pages into logical units for easier management, etc. Configuration Files: Smarty can assign variables pulled from configuration files. Template designers can maintain values common to several templates in one location without intervention from the programmer, and config variables can easily be shared between the programming and presentation portions of the application. Security: Templates do not contain PHP code. Therefore, a template designer is not unleashed with the full power of PHP, but only the subset of functionality made available to them from the programmer (application code.) Easy to Use and Maintain: Web page designers are not dealing with PHP code syntax, but instead an easy-to-use templating syntax not much different than plain HTML. The templates are a very close representation of the final output, dramatically shortening the design cycle. Variable Modifiers: The content of assigned variables can easily be adjusted at display-time with modifiers, such as displaying in all upper-case, html-escaped, formatting dates, truncating text blocks, adding spaces between characters, etc. Again, this is accomplished with no intervention from the programmer. Template Functions: Many functions are available to the template designer to handle tasks such as generating HTML code segments (dropdowns, tables, pop-ups, etc.), displaying content from other templates in-line, looping over arrays of content, formatting text for e-mail output, cycling though colors, etc. Filters: The programmer has complete control of template output and compiled template content with pre-filters, post-filters and output-filters. Resources: Templates can be pulled from any number of sources by creating new resource handlers, then using them in the templates. Plugins: Almost every aspect of Smarty is controlled through the use of plugins. They are generally as easy as dropping them into the plugin directory and then mentioning them in the template or using them in the application code. Many user-community contributions are also available. (See the plugins section of the forum and wiki.) Add-ons: Many user-community contributed Add-ons are available such as Pagination, Form Validation, Drop Down Menus, Calendar Date Pickers, etc. These tools help speed up the development cycle, there is no need to re-invent the wheel or debug code that is already stable and ready for deployment. (see the Add-ons section of the forum and wiki.) Debugging: Smarty comes with a built-in debugging console so the template designer can see all of the assigned variables and the programmer can investigate template rendering speeds. Compiling: Smarty compiles templates into PHP code behind the scenes, eliminating run-time parsing of templates. Performance: Smarty performs extremely well, despite its vast feature set. Most of Smarty's capabilities lie in plugins that are loaded on-demand. Smarty comes with numerous presentation tools, minimizing your application code and resulting in quicker, less error-prone application development/deployment. Smarty templates get compiled to PHP files internally (once), eliminating costly template file scans and leveraging the speed of PHP op-code accelerators. The main feature that has made Smarty PHP template programming so popular is that with Smarty the applications become a lot easier to use and maintain as web page designers are not dealing with PHP code syntax, but instead an easy-to-use templating syntax not much different than plain HTML. All this gained Smarty a reputation of a framework that is often used by far-sighted developers for long-run highly secure web projects. |
|||||||||||||||||||||||||||||
|
Why Smarty ??
One of Smartys primary design goals is to facilitate the separation of application code from presentation. Typically, the application code contains the business logic of your application, written and maintained in PHP code. This code is maintained by programmers. The presentation is the way your content is presented to the end user, which is written and maintained in template files. The templates are maintained by template designers. At its most basic function, the application code collects content, assigns it to the template engine and displays it. The content might be something like the headline, tagline, author and body of a newspaper article. The application code has no concern how this content will be presented in the template. The template designer is responsible for the presentation. They edit the template files, adding markup and bringing it to completion. This typically involves things like HTML tags, cascading style sheets and other tools provided by the template engine. |
|||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
|
Although application code is separated from presentation, this does not necessarily mean that logic is separated. The application code obviously has logic, but the templates may have logic based on the condition that it is for presentation only. For example, if the designer wants to alternate table row colors or upper-case some assigned content, they can. This is presentation logic, something the programmer should not be concerned with. How often have you had some presentation displayed in a single column and then you wanted it in two or three columns, so the application code needs adjusting to accomodate this? A better approach is to assign the content in one single array and let the template handle the presentation. This will simplify your application and keep your templates flexible. Smarty supplies the tools to handle this kind of situation. |
|||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||