Before you can grasp this tutorial you must have some basic knowledge of Smarty. I suggest you visit my previous tutorial on Smarty. Please note that I will be using the same file structure in this tutorial as the previous tutorial.

Plug-ins
Plug-ins can be used to add additional functionality to software and Smarty has provided a way for developers to create and add their own plug-ins. Smarty plug-ins are useful in the instances where you need to reuse code in multiple template files or need to avoid adding PHP directly to a template. Plug-ins can be a template function, modifier, compiler function or a block function.

Template Functions
Let's create a template function that will allow us to detect what browser the client is using and display an appropriate message.
Firstly you must create a new file in this directory: <Smarty root directory>\classes\plugins. Within this directory create a file called function.browser_detect.php. Smarty references functions by the name of the file so it is very important you name it exactly that.

{block name=body}
<p>Here's what we have to say about your browser: <strong>{browser_detect}</strong></p>
{/block}

Smarty's functions all require a specific format. Each template function name must start with smarty_function_ and then the name of the function. It should also accept two parameters, the first an array and the second the Smarty_Internal_Template object.

Array $parameters - This associative array is used to store the parameters being passed to the function from the template. e.g.

{browser_detect output_results=true}

Our parameter here is output_results so therefore to reference that without our browser_detect function we must use the following:

$parameters['output_results'];

Smarty_Internal_Template $smarty - This object is the Smarty object and it is passed by reference. This is useful in the instances where you need to assign values to placeholders from within your function.

Your function can do one of two things with it's results: return it or assign it to a placeholder. Our example returned its results. To assign our results to a placeholder consider the following:

//replace the return statement with this
$smarty->assign('message', $message);

ModifiersModifiers are plug-ins that allow the developer to 'modify' or process some content before displaying it. Modifiers can be applied on tags or placeholders.

We're now going to create a modifier that will hash some output with the desired algorithm and key. Creating the modifier file is similar to that of the function plug-in. Within the plugins folder mentioned earlier create a file named modifier.hash.php. Place this code within that file:

Every modifier function must has smarty_modifier in front of it's name just like smarty_function. A modifier function can take any amount of parameters but the first is a must. The first parameter is the string that will be modified. If we assign the text Hello World to the placeholder $text ($smarty->assign('text', 'Hello World');) then in our template file we will need this code to apply our hash modifier.

{$text|hash}

Since our hash modifier expects the algorithm and key we must also specify them.

{$text|hash:'MD5':'This is the key'}

Block Functions
Block functions are the functions that are executed on the contents of a block. A block function is created the same way a Smarty function and modifier are created.

Within the plugins folder create a file called block.nl2br.php. Add the following code:

This block function is called {nl2br}{/nl2br}. Basically what it does is converts all break lines to their HTML entity which is <br>. Let's break down the parameters:

array $parameters - Parameters to be passed from the template

mixed $content - String located between the opening and closing tags.

Smarty_Internal_Template $smarty - Instance of the Smarty object

boolean &$repeat - Boolean that lets you know whether you're handling the call on the opening or closing tag. Block functions are called twice, when the tag is opening and when it is closing. It defaults to TRUE on the opening tag but $content is NULL. It then defaults to FALSE on the closing tag while $content will contain the string to be manipulated.