adding modifier in smarty template engine
I found 2 ways to add custom modifier in smarty template engine. The first way is using ‘plugins’ directory and putting a php file with your modifier there. The second way is writing your own function and then registering it as modifier.
The first way: Creating custom plugin in plugins directory
Open smarty directory, there you can find ‘plugins’ folder with many files in there. You can notice they are all having standard structure in the filename: plugin type, then dot, plugin name, dot again and the file extension – "php"
To create your own modifier, you should write a new php script in the plugins directory and name it as modifier.yourmodifier.php, where yourmodifier is the alias of your modifier.
In the file, just specify function
function smarty_modifier_yourmodifier($input[, optional extra parameters]) {
// function body
return $input; // modify the return value in the way you want
}
And that’s all, you can use {$something|yourmodifier} in templates.
The second way is registering your modifier.
First, you define necessary function, for example
function mymodifier($input[, optional extra parameters]) {
// function body
return $input; // modify the return value in the way you want
}
Then register it using smarty object (variable name maybe another!)
$smarty->register('yourmodifier', 'mymodifier');
Note: the first parameter is the modifier name which will be used in smarty templates, the second parameter is the function you defined.
October 20th, 2009 at 7:49 pm
Hey, just wanted to mention that for me the “$smarty->register” didn’t work, but found that “$smarty->register_modifier” did work