void register_function (string name, mixed impl, bool cacheable, array or null cache_attrs)

Use this to dynamically register template function plugins. Pass in the template function name, followed by the PHP function name that implements it.


The php-function callback impl can be either (a) a string containing the function name or (b) an array of the form array(&$object, $method) with &$object being a reference to an object and $method being a string containing the mehod-name or (c) an array of the form array(&$class, $method) with $class being a classname and $method being a class method of that class.

执行函数的格式可以是一个包含函数名称的字符串;也可以是一个array(&$object, $method)数组形式,其中&$object是一个对象的引用,而$method是它的一个方法;还可以是一个array(&$ class, $method)数组形式,其中$class是一个类的名称,$method是类中的一个方法。

$cacheable and $cache_attrs can be omitted in most cases. See Controlling Cacheability of Plugins' Output on how to use them properly.

$cacheable$cache_attrs 参数在大多数情况下可以省略。它们的使用方法可以查看控 制插件输出的缓存

Example 13-22. register_function
例子 13-22. 注册函数

$smarty->register_function("date_now", "print_current_date");

function print_current_date ($params) {
$format="%b %e, %Y";
return strftime($format,time());

// now you can use this in Smarty to print the current date: {date_now}
// or, {date_now format="%Y/%m/%d"} to format it.

// 现在你可以在模板中这样显示日期:{date_now}
// 或者用{date_now format="%Y/%m/%d"}的格式进行格式化