简体   繁体   English

从tpl文件调用smarty插件函数时出错

[英]Error while calling a smarty plugin function from tpl file

Created an simple smarty plugin with below code 使用以下代码创建了一个简单的smarty插件

/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     function.assignmenu.php
 * Type:     function
 * Name:     assign
 * Purpose:  assign a value to a template variable
 * -------------------------------------------------------------
 */
functionality-to-smarty-w-plugins/

function smarty_function_assignmenu(Smarty_Internal_Template $smarty)
{
    //$template->assign($params['var'], $params['value']);
    $smarty->assign('test', '123');
}

And this function called from the template file as {assignmenu} {test} 然后此函数从模板文件中称为{assignmenu} {test}

While accessing the page, it will returns the following error Argument 1 passed to smarty_function_assignmenu() must be an instance of Smarty_Internal_Template, array given, called in /var/www/myproject/runtime/Smarty/compile/cda404646b2153274a9e77f736531c5dc7e6f91c_0.file.sample.tpl.php on line 31 and defined 访问页面时,它将返回以下错误参数1传递给smarty_function_assignmenu()必须是Smarty_Internal_Template的实例,给定数组,在/var/www/myproject/runtime/Smarty/compile/cda404646b2153274a9e77f736531c5dc7e6f91c_0.file.sample.tpl .php在第31行并已定义

I am using smarty 3 with yii2. 我正在使用yii2的smarty 3。 Any help is appreciated 任何帮助表示赞赏

Smarty plugin function expect an array as first parameter. Smarty插件函数期望将数组作为第一个参数。 So the definition should be 所以定义应该是

function smarty_function_assignmenu($paramArray,Smarty_Internal_Template $smarty)
{

    $smarty->assign('test', '123');
}

Even if there is no parameters to pass, the function definition should contain an array as first parameter. 即使没有要传递的参数,函数定义也应包含一个数组作为第一个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM