Why use Smarty? Benefits & Losses

Smarty is a templating engine, as everyone knows. It has a big market and is being used by companies like WHMCS for building great tools. It’s a great tool to be used in the development of projects of small and big levels.

Today, we are going to discuss about its benefits and losses. Who should prefer using smarty in his/her project and who should stay away and continue with PHP itself.

Some might argue that Smarty does what PHP can do already: separate the presentation from business logic. The PHP programming language is great for code development but when mixed with HTML, the syntax of PHP statements can be a mess to manage. Smarty makes up for this by insulating PHP from the presentation with a much simpler tag-based syntax. The tags reveal application content, enforcing a clean separation from PHP (application) code. No PHP knowledge is required to manage Smarty templates.

The importance of this separation is situational. It is commonly more important to web designers than it is to PHP developers. Therefore, Smarty is commonly a good fit when the roles of developers and designers are separated. There is no right or wrong answer: every development team has their own preferences for managing code and templates. Aside from a clean tag-based syntax, Smarty also offers a wide variety of tools to manage presentation: granular data caching, template inheritance and functional sandboxing to name a few. Business requirements and the PHP code Smarty is being used with will play a large role in determining if Smarty is a good fit.

In cases where efficient template management is crucial or the case where web designers (not PHP developers) are managing templates, the strengths of the tag-based template syntax are quickly realized. If the project size scales to hundreds or thousands of templates, template inheritance keeps template maintenance streamlined. What little Smarty adds technically (a tag-based syntax + 1-time compile step) is easily overcome by the time saved with template maintenance.

People who are looking for development of projects that can use multiple themes should opt for smarty and can do lot of good work with it. As I mentioned before, it is a template engine that can help you in keeping your PHP and Theme Files separate.

Who should not go for smarty?

People who do not want to implement separation in presentation and functional layers of the project should not look for Smarty.

People who are always looking for great community for support on programming languages and need lot of documentation on it, should not opt for it. Smarty has its own community and large documentation too but it can be a bit confusing if you require an instant action / solution, as you will not get a smarty developer easily.

Smarty is clearly not meant for every project. It is just one solution for managing the presentation of your PHP applications. Your business requirements along with your development team preferences will largely decide of Smarty is a good fit. Be sure to read All about Smarty, Use Cases and Work Flow, Template Inheritance and Syntax Comparison. And of course, install Smarty and give it a try.

How To Create Custom PHP Functions to use in Smarty Templates?

Smarty, the popular template engine that serves as a basis for many well-known content management systems such as Xoops, MovableType, and X-Cart, is full of great features. But it is not the most intuitive system for developers to learn to use, and it doesn’t help that the documentation is a bit scanty in spots. So here is the simplest and most complete tutorial you will find on writing a custom function for your Smarty templates.

Why You Need To Write Custom Functions

For most purposes, the built-in Smarty functions are sufficient. But sometimes, you will want to embed a custom function in your templates in order to define certain variables or display certain content. Now if you are wondering why you can’t just use PHP directly in your template, it is because there are some limitations on what you can do.

Yes, you can insert PHP into Smarty templates. You’d do it like this:

{php}
//php code here
echo "Hello World"; //prints out Hello World
{/php}

However, there are problems with this approach. Not only is this rather inelegant, but when you use PHP directly in your template, you defeat the purpose of using Smarty. But above all, there is one caveat: you cannot insert your own custom PHP functions directly within the php tags in a template. This is clearly a big problem for those of us who need to create customized components for a website.

But you can insert your own custom function if you create a Smarty function.

How To Create Your Own Smarty Function

Smarty makes it really easy to add a function. All you need to do is follow these 3 easy steps.

Step 1: Name Your Plugin and Add a Header

To start, you’ll need an empty PHP document. And then, in PHP comments, we add simple code which describes the plugin.

In this case, our plugin will get the length of a string that is passed to it and display output based on how long it is. And our plugin will be called vbchecker. See the sample code below.

<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     function.vbchecker.php
 * Type:     function
 * Name:     vbchecker
 * Purpose:  check string length and outputs a response
 * -------------------------------------------------------------
 */

Step 2: Write Your Function

Next, write the body of your function. There is one thing to pay attention to: all Smarty functions need to be preceded by “smarty_function_” in the function definition, as shown below. And, whether or not your function takes an input, you will always define it with two parameters: $params and &$smarty.

function smarty_function_vbchecker($params, &$smarty)
{
    $length = strlen($params['vbstring']);
    $output = "Your sentence is too long. Shorten It!";
    if($length < 50)
        $output = "Your sentence is just right!";
   
    return $output;
}
?>

And there you have it. You will notice that we referenced an external variable via the $params array.

Step 3: Save It In The Plugins Folder

Now, to add your new function to Smarty, you just need to save it in the plugins directory of your Smarty folder. (ie. Smarty-2.6.3 ⇒ plugins )

Be sure to save the plugin according to the file name that you specified in the header of your plugin.  

And that’s it! All that is left to do is use the new function in your template.

Calling Your New Function Within Your Template

To call your new function, you simply reference it by its name within curly brackets. And if the function requires a parameter, like ours does, you’d call it like this:

{vbchecker vbstring='See, it is very easy to write a function.'}

Obviously, this is a very simple example, but using these steps you can create very useful custom functions for your templates.