As an example, here’s how you would use register_outputfilter
to register an output filter at run time:
<?php include_once(‘libs/Smarty.class.php’); $smarty = new Smarty; function highlight($output, &$smarty) { // highlight the word “smarty” on our template source return str_replace(‘smarty’, ‘<b>smarty</b>’, $output); } $smarty->register_outputfilter(‘highlight’); $smarty->display(‘templates/example1.tpl’); ?>
As you can see, the PHP function is being registered at run time with Smarty, and it will return a modified version of the output, highlighting the word Smarty. While this is a very simple example, we will expand it soon with a more complex version to dynamically highlight search keywords.
You could also pass an array to register_outputfilter
(or any of the other register_*filter
functions) to tell Smarty to call the given class or object method, as shown below:
<?php include_once(‘libs/Smarty.class.php’); $smarty = new Smarty; class Misc { function highlight($output, &$smarty) { // highlight the word “smarty” on our template source return str_replace(‘smarty’, ‘<b>smarty</b>’, $output); } } $smarty->register_outputfilter(array(‘Misc’, ‘highlight’)); $smarty->display(‘templates/example1.tpl’); ?>
18.221.53.209