2015-04-01 48 views
0

在我的main.php布局文件中,Yii2,我需要渲染位于文件夹contacto/_form中的窗体。Yii2 - 渲染窗体内页脚(main.php)

我怎么能在$模型变量传递给文件main.php,内部布局的文件夹,并在使用它:

<?= $this->render('_form', [ 
    'model' => $model, 
]) ?> 

非常感谢。

回答

1

您可以像创建控件:

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

+0

非常感谢,但它总是抛出错误:未找到类'Widget'。我把类FormWidget extends Widget放在位于root/components的小部件中。有任何想法吗? – 2015-04-01 19:55:34

+0

使用yii \ base \ Widget; – user1852788 2015-04-01 20:15:06

+0

好的。它正在工作。非常感谢您指点正确的方式。 – 2015-04-01 20:36:18