關於Yii2中的MVC中的視圖總結(持續更新中)
阿新 • • 發佈:2017-10-17
gif pre lis frontend dbo register front reg open
一、首先在控制器中,將處理好的數據發送給前臺:
$this->layout = ‘base‘; 這裏填寫視圖的模板文件(可以不寫這行代碼,如果不寫,默認為views/layouts/main.php這個文件)
return $this->render (‘這裏填寫視圖文件名稱‘,[‘user‘=>這裏填寫處理好的數據(比如說數組$list等等)]);
如果不想使用模板文件,那麽這裏的寫法為 return $this->renderPartial(‘index‘);
前臺接受為$user
二、準備好模板文件:
<?php use yii\helpers\Html;View Codeuse yii\helpers\Url; use yii\web\View; $this->registerCssFile(‘@web/public/frontend/css/style.css‘); $this->registerJsFile(‘@web/public/frontend/js/jquery.min.js‘,[‘position‘=>View::POS_HEAD]); $this->registerJsFile(‘@web/public/frontend/js/index.js‘,[‘position‘=>View::POS_HEAD]); ?> <?php $this->beginPage()?> <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <?= Html::encode($this->title)?> <?= $this->head()?> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> </head> <body> <?php $this->beginBody()?> <?= $content?> <?php $this->endBody()?> </body> </html> <?php $this->endPage()?>
三、建立視圖文件
如果控制器是SiteController,那麽文件建立在views/site/(xxx.php),簡單來說,這個視圖文件,將被視為二中的$content,放入模板文件中。
關於Yii2中的MVC中的視圖總結(持續更新中)