2015-04-01 82 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