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:
[php]
{”This is just a string“|strip_tags}
{$description|truncate:40:”…”}
[/php]
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:
[php]
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Smarty plugin
*
* Type: modifier
* Name: grins
* Date: March 6, 2007
* Purpose: converts smilies to img tags
* Input:
* - string: input string to replace
* Example: {$text|grins}
* @version 1.0
* @author Jelena Pavlovic
* @param string
* @return string
*/
function smarty_modifier_grins($string)
{
$url = “images/”;
$signs = array(’:-)’, ‘:-(’, ‘;-)’, ‘:-|’, ‘:-S’ ,’:-P’ ,’:-$’,':-O’, ‘:-*’, ‘:-D’);
$replace = array(
‘
‘,
‘
‘,
‘
‘,
‘
‘,
‘
‘,
‘
‘,
‘
‘,
‘
‘,
‘
‘,
‘
‘
);
$string = str_replace($signs, $replace, $string);
return $string;
}
?>
[/php]
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:
[php]
include "includes/config.php";
$description = "This is one funny sentence. :-)
This is one sad sentence :-( “;
$smarty->assign(”description”, $description);
$smarty->display(”example-smilies.tpl.htm”);
?>
[/php]
and example-smilies.tpl.htm page and place it in templates dir:
[php]
{include file=”header.tpl.htm”}
Description without grins modifier applied
Description with grins modifier applied
{include file=”footer.tpl.htm”}
[/php]
Html code result will be:
[html]
Description without grins modifier applied
This is one sad sentence :-(
Description with grins modifier applied
This is one sad sentence
[/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 Smarty, Web, Web development

3 Responses to “Write Your Own Smarty Modifier”
More of these! Great example.
I am currently developing a backend in Smarty, and I didn’t realize the power of modifiers. I turned to Smarty because of its top handling with templates, which boosts my graphic possibilities.
I can’t wait for your next post. :D
Hmm, it sounds very easy, but i got every time following error message:
Fatal error: Smarty error: [in TEMPLATE line 43]: [plugin] modifier ‘trre’ is not implemented (core.load_plugins.php, line 118) in *\Smarty.class.php on line 1095
“trre” is the correct modifier name - it is only a copy of modifier.truncate.php.
greetings
tobias
okay,i found the error - it was my fault. when i write the modifier for project a i should put the modifier in the project a folder and not in project b folder … :whistle
Have something to say?