Custom Search

News World

Oct 3, 2012

Web Developer :: Smarty—The Ultimate 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.


Is Smarty Fast?
Template systems introduce another level of processing in the application, so we might suspect that Smarty applications will run slower than plain PHP equivalents. Basically, we have a new pseudo-scripting language (the template engine's tags) inside our scripting language (PHP).

Smarty is extremely fast by doing what is called template compiling. That means that it reads the templates, creates PHP scripts from them and includes the PHP files, resulting in a single PHP script that is compiled by the PHP engine, which is pretty fast. The beauty of the process is that the templates are parsed only once by Smarty, and only templates that are modified are compiled again. This is done automatically by Smarty and results in a fast compilation done by the PHP engine with very little overhead from Smarty.

If you are concerned about the performance of your site, Smarty has built-in caching support that can speed things up, especially on websites that have content that is not modified very often. You can cache all the content of a web page or only some of it and you can specify for how long Smarty should keep the content cached. This will be explained in more detail on the next pages.

Since Smarty does template compiling resulting in a PHP script, there is no reason why we should not use a PHP compiler cache solution like PHP Accelerator or Zend Cache.

PHP Accelerator and Zend Cache have no problem with Smarty's output and cache the PHP scripts produced by Smarty very well, so if our main concern is performance we should use one of these caching solutions combined with Smarty's built-in caching support.





Is Smarty Secure?
We concluded that Smarty is fast, but we need to know about its security, which according to me is most important for any website.

By default, when using Smarty you are theoretically as secure as you are when using only PHP. This means that by using Smarty you can't be less secure than using PHP only; but Smarty has some features to improve security in case this is needed.

When a site is built by a programmer working with a designer without using Smarty, the designer has access to the application and can modify all the PHP scripts. This is not good for security because a designer with bad intentions can breach the security of the system very easily when he or she has all the power of PHP in his or her hands.

Smarty comes with some built-in security features for situations where unreliable parties edit templates. When security is set on templates, a designer who modifies templates via an unsecured connection like FTP is unlikely to gain access in the system.


Using Smarty security features, you can:

  • Allow or deny usage of PHP code in the templates
  • Allow only a set of PHP functions as variable modifiers
  • Restrict the folders from which templates can be included
  • Restrict the folders from which local files can be fetched by templates

My strong advice is to always think about security. Think about having a database with credit card details and your designer including unsafe PHP code in one web page. This would result in a disaster for your business.



Smarty's Main Features
Smarty offers both designers and programmers tools to optimize their work. Reading this entire book you will find out about all these wonderful features and will see how great Smarty is. Let's preview some of these features and look at the reasons why they should or should not be used.


Variable Modifiers
When displaying content on a website, you might want to change some of it depending on the time or visitor's origin. For example, we might want to show a date in different formats. Using Smarty, there's no need for the programmer's intervention for this. He or she will just pass the date in the proper variable to the template, and the designer can format the date however he or she desires.

Also, the designer can upper-case or lower-case a variable, truncate a text block, add spaces between characters, and so on and it's all done at display-time with Smarty.

Think about displaying a product category as a small title in uppercase and with spaces between the word's characters (for example, 'C A R S'). In the database, the corresponding column contains the word 'cars'. With Smarty you don't have to ask the programmer to change it to 'C A R S'. When he or she passes the variable to the template, you can do that with proper variable modifiers at display-time, and again, if you want to modify the way you display the product category later (for example, 'Cars'), this doesn't require the programmer's intervention.


Template Functions
While going through Smarty's syntax you will discover another great thing about Smarty—Template Functions. Think about designing a large form with many HTML dropdowns. The old-fashioned way to do this is to write tags for every drop-down menu. With Smarty, you can simply write a function for dropdowns and call it every time you need to display one, making things simpler and faster. This was an easy example, but you can save a lot of time writing functions for displaying content in manners that you repeat frequently.


Debugging
At every step of building a software application, programmers and designers need debugging to easily correct their work.
Think how much time you can lose if you misspell a variable and don't have debugging tools. In my experience as a programmer, that's the worst scenario, even worse than having mistakes in your algorithm.
Even if you can debug with PHP, Smarty provides a debugging console for correcting Smarty-related errors. This is a very powerful debugging tool, as you are informed of all the included templates, assigned variables and configuration file variables for the current template.
With Smarty you can modify the format of the debugging console, so you can highlight the things you find important in debugging. In addition, you can dump the output of the debugging console to the page using a Smarty function in the template, but since Smarty's functions are executed at run time, you can only see the variables involved not the templates included.


Plug-ins
One very important thing for a business that develops software is the possibility to reuse code. This saves time and money when new projects are built that contain functionality related to other projects created before, and this is what really makes a software business profitable. Using code from one application in another with copy and paste requires modification of variables, function names, retesting and careful integration with the new application, but is faster rather than rewriting the piece of code.
However, there is a better method for reusing code—using Plug-ins. When building a site, identify functionalities that can be reused even in the same project and create them as plug-ins. This way, after building a few sites, you will have a portfolio of plug-ins that run well and can be included without modifications in any new project saving a lot of time and work. For doing this, we need to have plug-in support in the software that we use to build our project.


Smarty has a plug-in architecture that is used for most of its customizable functionality. With Smarty, you can write plug-ins for functions, compiler functions, block functions, modifiers, resources, inserts, prefilters, postfilters, and output filters.

A lot of software out there with plug-in architecture loads all plug-ins before compiling. This is not necessarily wrong, but Smarty's designers kept performance in mind and made the plug-ins load only when they are invoked in a template. More than that, a plug-in invoked once in a template is only loaded once, even if there are more instances of Smarty requesting the same plug-in.

For example, when creating an e-commerce site, you can create plug-ins for the shopping basket functionality or filters for currency conversion that you can use later in other e-commerce projects you build, without having to write the same code again but being paid similar amounts of money.




Filters
Disciplined designers may write a lot of comments in their templates, resulting in a big file after compilation. Smarty's developers thought about this and included a set of filters to solve this problem. Smarty has prefilters, which are PHP functions that the templates are run through before they are compiled. With prefilters you can modify anything in the templates before template compilation. For example, if you want to remove all the comments you can do that very easily using a prefilter.

However, if you want to add some comments to the compiled templates you can do that with postfilters. Smarty's postfilters are PHP functions that the templates are run through after they are compiled.

You can also filter out the content of a template by using output filters, which are PHP functions used for filtering the output of a template when it is executed, for example replacing banned words with *, hiding email addresses, and so on. With these powerful filters, the programmer has complete control over the templates.


1 comment:

Shasing19 said...

I just take a peek and I discovered your blog regarding templating system. I appreciated the effort you've put in your post and I am very glad I came across here.

IT Conversations

Moneycontrol Latest News

Latest new pages on Computer Hope

Latest from Infoworld

Door Lock

Door Lock Import Top Door Lock from China Contact Quality Manufacturers Now