Write Your Own Smarty Modifier
March 11th, 2007 by morning
I already wrote short tutorial for beginners which covers installation and some common usage of smarty tags.
Modifiers are basically php functions (custom or predefined) which work with variables, custom functions and strings. That means, you can actually call php functions as modifiers. Applying a modifier is very easy, all you need to specify the value followed by pipe and the modifier name. Some of the modifiers can be called with additional parameters. For example:
<!-- Or -->
{$description|truncate:40:"…"}
Even though Smarty covers lots of very useful modifiers, sometimes, you will have a need for new one.
Let’s write a simple modifier which will be applied to strings and will replace smilies shortcuts with actual images.
Create a file named: modifier.grins.php and place it within your Smarty dir in /libs/plugins/
If you open any existing modifier, you will see a phpdocumentor compatible comment which describes modifier. It is recommendable that you use the same when writing your own ones.
For replacing smilie shortcuts with actual images, I will use simple str_replace php function, so the result would be:
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Smarty plugin
*
* Type: modifier<br>
* Name: grins<br>
* Date: March 6, 2007
* Purpose: converts smilies to img tags
* Input:<br>
* - string: input string to replace
* Example: {$text|grins}
* @version 1.0
* @author Jelena Pavlovic <jelena@arraystudio.com>
* @param string
* @return string
*/
function smarty_modifier_grins($string)
{
$url = "images/";
$signs = array(':-)', ':-(', ';-)', ':-|', ':-S' ,':-P' ,':-$',':-O', ':-*', ':-D');
$replace = array(
'<img src="'.$url.'smiley_1.gif" alt=":-)" />',
'<img src="'.$url.'smiley_2.gif" alt=":-(" />',
'<img src="'.$url.'smiley_3.gif" alt=";-)" />',
'<img src="'.$url.'smiley_4.gif" alt=":-|" />',
'<img src="'.$url.'smiley_5.gif" alt=":-S" />',
'<img src="'.$url.'smiley_6.gif" alt=":-P" />',
'<img src="'.$url.'smiley_7.gif" alt=":-$" />',
'<img src="'.$url.'smiley_8.gif" alt=":-O" />',
'<img src="'.$url.'smiley_9.gif" alt=":-*" />',
'<img src="'.$url.'smiley_10.gif" alt=":-D" />'
);
$string = str_replace($signs, $replace, $string);
return $string;
}
?>
What you have probably noticed is function name, which has to follow the pattern of smarty(underscore)modifier(underscore)chosenName, where chosenName will be used to call your modifier.
To apply this modifier we can create example-smilies.php page with:
include "includes/config.php";
$description = "This is one funny sentence. :-) <br />This is one sad sentence :-( ";
$smarty->assign("description", $description);
$smarty->display("example-smilies.tpl.htm");
?>
and example-smilies.tpl.htm page and place it in templates dir:
Html code result will be:
<head>
<title></title>
</head>
<body>
<h2>Description without grins modifier applied</h2>
<div>This is one funny sentence. :-) <br />This is one sad sentence :-( </div>
<h2>Description with grins modifier applied</h2>
<div>This is one funny sentence. <img src="images/smiley_1.gif" alt=":-)" /> <br />This is one sad sentence <img src="images/smiley_2.gif" alt=":-(" /> </div>
</body>
</html>
Very simple. 
I have updated .zip file which contains example from previous tutorial with those described here, so feel free to download it.
- Posted in Web, Smarty, Web development
- 3 Comments »
