Smarty3.0用registerPlugin替换2.0的register_function
今天用Smarty做一个简单的网站中常用功能框架,用的Smarty模板引擎,下了个最新的3.0版本,在使用$smarty->register_function(“FormatTextarea”,”format_textarea”)注册函数时,发现居然提示”Notice: function call ‘register_function’ is unknown or deprecated. in E:\webphp\Smarty\sysplugins\smarty_internal_wrapper.php on line 57″。
到Smarty中的sysplugins文件夹下查找,奇怪居然姜register_function方法取消了,这也是模板引擎中最常用的方法了,为什么会取消了,应该会有替代的方法吧,仔细在该文件加下找了半天,发现了一个smarty_internal_register.php一个系统插件,里面有一个方法如下:
|
01
02
03
04
05
06
07
08
09
10
|
public function registerPlugin($type, $tag, $callback, $cacheable = true, $cache_attr = null){ if (isset($this->smarty->registered_plugins[$type][$tag])) { throw new Exception("Plugin tag \"{$tag}\" already registered"); } elseif (!is_callable($callback)) { throw new Exception("Plugin \"{$tag}\" not callable"); } else { $this->smarty->registered_plugins[$type][$tag] = array($callback, (bool) $cacheable, (array) $cache_attr); }} |
花了点时间研究了一下,完全可以使用 $smarty->registerPlugin(“function”,”FormatTextarea”,”format_textarea”)代替smarty2.0中的$smarty->register_function(“FormatTextarea”,”format_textarea”)。
试了一下,果然成功,发出来给大家共享一下:
index.php
|
1
2
3
4
5
6
7
|
Function format_textarea($params, &$smarty){ extract($params); //.... return "Test \$smarty->registerPlugin()";}$smarty->registerPlugin("function","FormatTextarea","format_textarea");$smarty->display("test.tpl"); |
test.tpl
|
1
|
{FormatTextarea param = "params"} |
输出结果为:
Test $smarty->registerPlugin()
浙公网安备 33010602011771号