在Smarty模板引擎中,如何解决自定义函数传值问题?

更新于
2026-09-27 14:07:49
2阅读来源:SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计220个文字,预计阅读时间需要1分钟。

在Smarty模板引擎中,如何解决自定义函数传值问题?

使用Smarty的`$smarty->registerPlugin()`方法自定义函数时,需要在自定义函数执行完毕后将结果传递给模板。直接查找方法时,容易找不到正确的方法。查阅Smarty官方文档后,发现只需在自定义函数中增加一个返回值即可。

在Smarty模板引擎中,如何解决自定义函数传值问题?

Smarty 的 $smarty->registerPlugin()方法自定义函数时,需要将自定义的函数执行以后的结果传值给tpl模板,一直找不到正确的方法。查询Smarty官方文档后,发现只需要在自定义的函数中增加一个$template参数即可实现。

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的`$smarty->registerPlugin()`方法自定义函数时,需要在自定义函数执行完毕后将结果传递给模板。直接查找方法时,容易找不到正确的方法。查阅Smarty官方文档后,发现只需在自定义函数中增加一个返回值即可。

在Smarty模板引擎中,如何解决自定义函数传值问题?

Smarty 的 $smarty->registerPlugin()方法自定义函数时,需要将自定义的函数执行以后的结果传值给tpl模板,一直找不到正确的方法。查询Smarty官方文档后,发现只需要在自定义的函数中增加一个$template参数即可实现。

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}