问题描述
我正在 PHP 中寻找解决方案来使用一个函数,该函数可以使用多个参数而无需事先定义参数的数量。我知道有函数 func_get_args(),但这还不够......在我的例子中,我有一个用树枝渲染模板的函数,我想为模板提供上下文,我已经做到了,但它不是真的很合适,因为如果我调用函数时参数的顺序发生变化,一切都会被破坏......
有人有解决方案吗?... 感谢您的时间!
public function render_with_context()
{
$args = func_get_args();
$context['loged'] = $args[0];
$context['agencies'] = $args[1];
$context['login'] = $args[2];
$context['password'] = $args[3];
$context['active_agencies'] = $args[4];
\Timber\Timber::render('views/admin.html.twig',$context);
}
解决方法
我做到了,效果非常好!
$this->render_with_context([
'loged' => $loged,'agencies' => $agencies,'login' => $bdd_login,'password' => $bdd_password,'active_agencies' => $active_agencies
]);
public function render_with_context() {
$args = func_get_args();
$context['loged'] = $args[0]['loged'];
$context['agencies'] = $args[0]['agencies'];
$context['login'] = $args[0]['login'];
$context['password'] = $args[0]['password'];
$context['active_agencies'] = $args[0]['active_agencies'];
\Timber\Timber::render('views/admin.html.twig',$context);
}