Monday, June 12, 2006

Smarty Classes

Smarty classes are PHP classes which have one basic purpose, "To separate the business logic and presentation logic". Using smarty classes, we can write separate code for the business logic in PHP. Usually presentation logic is written in HTML which is echoed from PHP. How many times have we wished that we can use PHP in the presentation logic part to obtain more control over how the data is displayed? Smarty is the appropriate answer to this, for it enables to write embedded PHP code in the HTML level, with all the frills of HTML along with the control logic of PHP

Though Smarty is known as a "Template Engine", it would be more accurately described as a "Template/Presentation Framework". That is, it provides the programmer and template designer with a wealth of tools to automate tasks commonly dealt with at the presentation layer of an application. I stress the word Framework because Smarty is not a simple tag-replacing template engine. Although it can be used for such a simple purpose, its focus is on quick and painless development and deployment of your application, while maintaining high-performance, scalability, security and future growth.

Some of the exciting features of Smarty include:

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-cacheable, 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.)

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.

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: Although the functionality of Smarty is quite extensive, it still performs extremely well. Most of Smarty's capabilities lie in plugins that are loaded on-demand. Many of Smarty's features would be necessary in your application anyways, resulting in quicker development/deployment. Smarty templates get compiled to PHP internally, so they take advantage of PHP op-code accelerators.

Hope this was useful.

For more info on Smarty or to get your hands dirty with Smarty go here smarty.php.net