【发布时间】:2015-04-01 17:14:34
【问题描述】:
在 Yii2 的 main.php 布局文件中,我需要渲染一个位于文件夹 contacto/_form 中的表单。
我如何将 $model 变量传递给文件 main.php,在 layouts 文件夹中,并在以下位置使用它:
<?= $this->render('_form', [
'model' => $model,
]) ?>
非常感谢。
【问题讨论】:
在 Yii2 的 main.php 布局文件中,我需要渲染一个位于文件夹 contacto/_form 中的表单。
我如何将 $model 变量传递给文件 main.php,在 layouts 文件夹中,并在以下位置使用它:
<?= $this->render('_form', [
'model' => $model,
]) ?>
非常感谢。
【问题讨论】:
您可以这样创建小部件:
class FormWidget extends Widget
{
/**
* @return string
*/
public function run()
{
$model = ...;// code to create model
return $this->render('_form', [
'model' => $model
]);
}
}
在layout 这样的输入小部件中:
<?= FormWidget::widget() ?>
有关创建小部件的更多信息 - http://www.yiiframework.com/doc-2.0/guide-structure-widgets.html#creating-widgets
【讨论】: