Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement