在Smarty模板引擎中,如何解决自定义函数传值问题?
- 内容介绍
- 文章标签
- 相关推荐
本文共计220个文字,预计阅读时间需要1分钟。
使用Smarty的`$smarty->registerPlugin()`方法自定义函数时,需要在自定义函数执行完毕后将结果传递给模板。直接查找方法时,容易找不到正确的方法。查阅Smarty官方文档后,发现只需在自定义函数中增加一个返回值即可。
1.[代码][PHP]代码
function fn_display($_id, $template) { $_return = "test"; $template->assign("display", $_return); } $this->obj_smarty = new Smarty(); //初始化 Smarty 对象 $this->obj_smarty->registerPlugin("function", "call_display", "fn_display"); //注册自定义函数
2.[代码][PHP]代码
{call_display call_id=7} {$display}
本文共计220个文字,预计阅读时间需要1分钟。
使用Smarty的`$smarty->registerPlugin()`方法自定义函数时,需要在自定义函数执行完毕后将结果传递给模板。直接查找方法时,容易找不到正确的方法。查阅Smarty官方文档后,发现只需在自定义函数中增加一个返回值即可。
1.[代码][PHP]代码
function fn_display($_id, $template) { $_return = "test"; $template->assign("display", $_return); } $this->obj_smarty = new Smarty(); //初始化 Smarty 对象 $this->obj_smarty->registerPlugin("function", "call_display", "fn_display"); //注册自定义函数
2.[代码][PHP]代码
{call_display call_id=7} {$display}

