将Smarty中的几个变量传递给PHP函数


Pass several variables from Smarty to PHP function

我们有一个与Smarty一起工作的商店系统。我需要将一些Smarty变量传递给PHP函数并获得返回。

到目前为止,我所知道的和所做的是:

{$order_id|@get_order_total}

因此,它将Smarty变量"$order_id"传递到包含函数get_order_total($order_id)的PHP文件中,并向我显示该函数的返回。

现在我需要将3个变量传递给一个PHP函数。例如,该函数如下所示:

handleDebit($order, $payCode, $insertId)

遗憾的是,到目前为止,我还没有在智能文档中找到正确的东西。有人做过这种事吗?

如果您真的需要从smarty模板中调用函数,请将包装函数注册为smarty插件:

<?php
$smarty->registerPlugin("function","handleDebit", "handleDebitSmarty");
function handleDebitSmarty($params, $smarty)
{
  return handleDebit($params['order'], $params['payCode'], $params['insertId']); 
}

现在你可以用它作为智能标签:

{handleDebit order=$blah payCode=$blub insertId=$yeahh}

但您应该考虑@JonSterling的建议,并尝试找到一种方法,即控制器正在执行handleDebit-调用,而您只处理模板中的结果/显示内容。