Share on Google+Share on Google+

What is Smarty Templates

This articles gives brief description of the Smarty Template for PHP.

What is Smarty Templates for PHP?

Smarty is a template engine for PHP or web template system written in PHP language. Monte Ohrt and Andrei Zmievski jointly developed this smarty template engine. The difference between Smarty and other template engines like FastTemplate and patTemplate is that Smarty compiles the templates into PHP scripts, eliminating the operating cost incurred in parsing the templates whenever they are accessed. This makes Smarty very scalable for large applications and would work well in stressful and high-traffic conditions!

What is template compilation?

Smarty parses the templates and creates PHP scripts. When a web page is accessed, PHP executes the compiled smarty file instead of pulling the templates, which saves the work of having to parse the templates again. It only re-compiles the templates when any changes are occur to them, that's why we don't have to  manually compile the templates.

What are the features of Smarty Templates Engine?

The main features of Smarty Templates engine are:

  1. Caching: Smarty features built-in caching of template outputs. The data source would usually be external and slow, and is often the bottleneck in any application, like a remote database. Caching is used to speed up a call to display() or fetch() by saving its output to a file.

Templates are dynamic, so it is important to be careful what are caching and the duration. For example, if any website displaying it's front page that does not change its content very often, it might work well to cache this page for an hour or more. On the other hand, if a website is displaying a page with a weather map containing new information by the minute, it would not make sense to cache this page.

Setting up Caching



$smarty=new Smarty;




With caching enabled ($smarty->caching=true;), the function call to display('xyz.tpl') will render the template as usual, but also saves a copy of its output to a file (a cached copy) in the $cache_dir Upon the next call to display('xyz.tpl'), the cached copy will be used instead of delivering the template again.

The caching functionality is totally different from that of Zend Cache, PHP Accelerator etc. Caching tools like PHP Accelerator cache the complied bytecode of  PHP scripts, on the other hand Smarty caches the output of templates. As such, Smarty can work hand in hand with Zend Cache, where Zend Cache would cache the PHP scripts that Smarty creates from user templates. 

    2. Variable Modifiers: Smarty provides variable modifiers which allows us to modify the content of the variable, like:

    i) {$title| upper}: Convert the whole content of $title into uppercase.

    ii) {$content | truncate:30}: Display first 30 characters of $content followed by ' ... '

    3. Template Functions:

    Smarty provides two types of functions: 

        i) Built-in functions

        ii) Custom functions 

These functions are like the API of Smarty templates, as the name indicates custom functions can be modified but not built-in functions. These functions allow to do things like program conditional output (using if statements), perform iteration with dynamic loops (using foreach or section). 

    4. Filters:

  • Prefilters: Removes unwanted comments (usually created by IDEs )

  • Postfilters: We can add additional information like creation date etc.

  • Outputfilters: Modify the template output. By using preg_replace() method we can obfuscate the e-mail address

    5.Config. files: Configuration files are used for storing global template variables. One good example of such variable would be the colour scheme of a template. The section names are enclosed in brackets (e.g. [home_page]) and are only loaded upon request. Anything that's not in a section is globally available (upon a call to the config_load function).

    6. Plug-ins: With Plug-ins we can develop our template functions, variables, modifiers, and filters. Some of plug-in types are modifier, block, compiler, resource, insert, prefilters, postfilters, and outputfilters.



Posted on: October 6, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null