码迷,mamicode.com
首页 > Web开发 > 详细

关于Yii2中的MVC中的视图总结(持续更新中)

时间:2017-10-17 14:58:55      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:dex   size   ase   color   type   enc   doctype   title   总结   

一、首先在控制器中,将处理好的数据发送给前台:

$this->layout = ‘base‘; 这里填写视图的模板文件(可以不写这行代码,如果不写,默认为views/layouts/main.php这个文件)

return $this->render (‘这里填写视图文件名称‘,[‘user‘=>这里填写处理好的数据(比如说数组$list等等)]);

如果不想使用模板文件,那么这里的写法为 return $this->renderPartial(‘index‘);

前台接受为$user

二、准备好模板文件:

技术分享
<?php 
use yii\helpers\Html;
use 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()?>
View Code

三、建立视图文件

如果控制器是SiteController,那么文件建立在views/site/(xxx.php),简单来说,这个视图文件,将被视为二中的$content,放入模板文件中。

关于Yii2中的MVC中的视图总结(持续更新中)

标签:dex   size   ase   color   type   enc   doctype   title   总结   

原文地址:http://www.cnblogs.com/fpcing/p/7680499.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!