M/s VIKASH TECH - We Write Imaginations to Codes..!!

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

Smarty

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.

Category :

Comments

No comments found.

Leave a Comment

Your email address will not be published. Required fields are marked *


Become An Affiliate

Latest Posts

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…

best practices

WordPress best practices to make your website fast, user-friendly, and more reliable.

We know you’re going to take on some incredible work next year, so before you get too crazy with new…

Smarty

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,…

Software development

Software Development after Covid-19: A requirement not a choice.

The COVID-19 has turned consumer behavior inside out. We order groceries online and takeout from restaurants. Retailers offer online ordering with…

Tags