[英]How to access variables from controller in layout/main.php Yii2
[英]How to put yiibooster's textField inside /view/layout/main.php in Yii
我正在使用YiiBooster並嘗試在/views/layouts/main.php中的TbActiveForm中創建一個TextField
對於我添加的控制器:
<?php
class LayoutsController extends Controller
{
public function actionMain()
{
$model=new Item;
$this->render('main',array('model'=>$model));
}
}
?>
意見:
<?php
$this->beginWidget(
'booster.widgets.TbActiveForm',
array(
'id' => 'inlineForm',
'type' => 'inline',
'htmlOptions' => array('class' => 'well'),
)
);
echo $form->textFieldGroup($model, 'textField');
$this->endWidget();
?>
但我有小問題,當試圖運行它時出現錯誤信息:
PHP notice Undefined variable: model
任何人都可以幫我解決這個問題嗎? 謝謝。
要點:1
如果你只使用$ this-> widget,那么表單的輸入元素(例如textFields,textAreas,dropdownlists,checkBoxes等)將放在表單的旁邊。 喜歡
<form method="post" action="#">
</form>
<!-- and then the input elements of form, like -->
<input type="text" name="textField"> <!-- and so on.... -->
要點:2
要在表單中包含元素,您必須從中開始
$this->beginWidget
// then the input elements , and finally
$this->endWidget();
所以現在HTML看起來像
<form method="post" action="#">
<input type="text" name="textField"> <!-- and so on.... -->
</form>
要點:3
您必須將beginWidget分配給變量( $ form )以包含輸入元素
以下示例
(i) 控制器功能
public function actionFunctionName()
{
$model=new ModelClassName;
$this->render('viewFileName',array('model'=>$model));
}
(ii) 在視圖文件中
<?php
$form=$this->beginWidget(
'booster.widgets.TbActiveForm',
array(
'id' => 'inlineForm',
'type' => 'inline',
'htmlOptions' => array('class' => 'well'),
)
);
echo $form->textFieldGroup($model, 'textField');
// before the close tag of php
$this->endWidget();
?>
它工作正常。
要點:4
如果它不適合您,請檢查您的YiiBooster配置。 我希望它對你有所幫助。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.