function strlenSmarty($params) { return strlen($params['str']); } $smarty->register_function('strlen', 'strlenSmarty');
The length of "Hello world!" is {strlen str="Hello world!"}.
The length of "Hello world!" is 12.
function randSmarty($params) { $min = $params['min']; $max = $params['max']; return mt_rand($min, $max); } $smarty->register_function('rand', 'randSmarty');
A random number between 1 and 10 is {rand min=1 max=10}.
A random number between 1 and 10 is 6.In both of these examples, the functions we register with Smarty are fairly simple and don't require any external libraries or packages. However, it's possible to use the register_function method to integrate more complex functionality from third-party libraries or custom classes. It all depends on the needs of your specific application.