31
Smarty—The Ultimate Templating System for PHP
Filed Under (Smarty) by Ramin on 31-07-2009
Tagged Under : Smarty, Smarty—The Ultimate Templating System for PHP, Templating System for PHP
Now that we’ve decided to make our life easier by considering multi-tier architecture for our website, we should take a look at what Smarty can do, as well as what it can’t do for us.
Smarty is not built for separating HTML from PHP; instead, its primary goal is to separate the application logic from presentation logic. If Smarty’s goal was to separate PHP from HTML then the presentation layer would contain no logic; but Smarty templates can contain logic, as long as it is to be used for presentation only.
This may seem like it breaks the rules of rigid separation between layers and tasks, but there are good practical reasons for it. A simple example would be an e-commerce site with products displayed in four columns:
- Without Smarty: If our presentation layer doesn’t contain logic, we need to modify the business logic to retrieve the products in four arrays.
- With Smarty: Using logic in the templates, the programmer just passes the products to the templates in a single array and the designer arranges the products in the page as he or she desires.
Smarty offers an interface to pretty much all of PHP, so PHP can be included in the templates, but it’s recommended to leave most of PHP code at the business logic layer. Fortunately, Smarty’s logic is generally much simpler to use than PHP and designers do not need to become programmers in order to incorporate presentation logic into their Smarty designs.